Skip to content

Commit

Permalink
feature: data-files (yaml + json) can be used
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeMitterer committed Apr 14, 2015
1 parent 7e26392 commit 8beec7a
Show file tree
Hide file tree
Showing 14 changed files with 221 additions and 5 deletions.
30 changes: 29 additions & 1 deletion README.md
Expand Up @@ -24,22 +24,30 @@ Before you read on - check out this video:
│   │   ├── markdown.md
│   │   ├── piratenames.json
│   │   └── xtreme.html
│   └── _templates
│ │
│   ├── _data
│   │   ├── xmen.yaml
│   │   └── families.json
│ │
│ └── _templates
│   ├── default.html
│   └── info_page.html
└── web
├── about
│   ├── index.html
│   └── packages -> ../../packages
├── index.html
├── main.dart
├── markdown.html
├── packages -> ../packages
├── piratenames.json
├── styles
│   ├── main.css
│   ├── main.scss
│   └── packages -> ../../packages
└── xtreme.html
```

Expand All @@ -58,6 +66,26 @@ The following file-formats are supported:
- .html
- .scss
- .css

**html/_data**: [optional] This is the place where you can store your data-files.
The following file-formats are supported:

- .yaml
- .json

**SiteGen** injects your data into a global _data variable.
Here is a sample how to use such data:

```html
<ul>
{{#_data.xmen}}
<li>{{.}}</li>
{{/_data.xmen}}
</ul>
```

**SiteGen**

**html/_templates**: The directory containing your HTML+Mustache templates.

Expand Down
13 changes: 13 additions & 0 deletions example/simple/html/_content/families.html
@@ -0,0 +1,13 @@
title: Families
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<ul>
{{#_data.families}}
<li>{{surname}}
<ul>
{{#members}}
<li>{{given}}</li>
{{/members}}
</ul>
</li>
{{/_data.families}}
</ul>
7 changes: 7 additions & 0 deletions example/simple/html/_content/xmen.html
@@ -0,0 +1,7 @@
title: X-Men
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<ul>
{{#_data.xmen}}
<li>{{.}}</li>
{{/_data.xmen}}
</ul>
17 changes: 17 additions & 0 deletions example/simple/html/_data/families.json
@@ -0,0 +1,17 @@
[
{
"surname": "Jones",
"members": [
{"given": "Jim"},
{"given": "John"},
{"given": "Jill"}
]
},
{
"surname": "Smith",
"members": [
{"given": "Steve"},
{"given": "Sally"}
]
}
]
7 changes: 7 additions & 0 deletions example/simple/html/_data/xmen.yaml
@@ -0,0 +1,7 @@
- Professor X
- Beast
- Colossus
- Cyclops
- Iceman
- Marvel Girl
- Storm
2 changes: 1 addition & 1 deletion example/simple/html/_templates/default.html
Expand Up @@ -31,7 +31,7 @@ <h2>{{title}}</h2>

<!-- Values set in 'site.yaml' can be accessed as properties in the '_site' variable -->
by <span>{{_site.author}}</span>
Template: {{template}}
Template: {{_template}}
</footer>
<script type="application/dart" src="{{_page.relative_to_root}}/main.dart"></script>
<script src="packages/browser/dart.js"></script>
Expand Down
5 changes: 3 additions & 2 deletions example/simple/web/about/index.html
Expand Up @@ -7,7 +7,7 @@
<head>
<title>About | HudriWudri</title>
<meta charset="utf-8">
<link rel="stylesheet" href="../styles/main.css">
<link rel="stylesheet" href="..//styles/main.css">
</head>
<body>
<div class="menu">
Expand Down Expand Up @@ -36,8 +36,9 @@ <h2>Headline II</h2>
<span>Last modified: 30.03.2015</span>
<!-- Values set in 'site.yaml' can be accessed as properties in the '_site' variable -->
by <span>Mike Mitterer</span>
Template: /Volumes/Daten/DevLocal/DevDart/SiteGen/example/simple/html/_templates/default.html
</footer>
<script type="application/dart" src="../main.dart"></script>
<script type="application/dart" src="..//main.dart"></script>
<script src="packages/browser/dart.js"></script>
</body>
</html>
50 changes: 50 additions & 0 deletions example/simple/web/families.html
@@ -0,0 +1,50 @@
<!-- Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
for details. All rights reserved. Use of this source code is governed by a
BSD-style license that can be found in the LICENSE file.
-->
<!DOCTYPE html>
<html>
<head>
<title>Families | HudriWudri</title>
<meta charset="utf-8">
<link rel="stylesheet" href="/styles/main.css">
</head>
<body>
<div class="menu">
<a href="/index.html">Home</a> |
<a href="/markdown.html">Markdown</a> |
<a href="/xtreme.html">XTreme simple</a> |
<a href="/about/index.html">About</a>
</div>
<!-- 'title' defaults to markdown filename if not defined in the file's YAML block -->
<h2>Families</h2>
<!-- '_content' contains the contents of the markdown file (minus the yaml variables in the header) -->
<ul>
<li>Jones
<ul>
<li>Jim</li>
<li>John</li>
<li>Jill</li>
</ul>
</li>
<li>Smith
<ul>
<li>Steve</li>
<li>Sally</li>
</ul>
</li>
</ul>
<footer>
<!--
'_date' stores the markdown page's last modified date. The date formatting can be set in 'site.yaml' via the
'date_format' variable.
-->
<span>Last modified: 14.04.2015</span>
<!-- Values set in 'site.yaml' can be accessed as properties in the '_site' variable -->
by <span>Mike Mitterer</span>
Template: /Volumes/Daten/DevLocal/DevDart/SiteGen/example/simple/html/_templates/default.html
</footer>
<script type="application/dart" src="/main.dart"></script>
<script src="packages/browser/dart.js"></script>
</body>
</html>
1 change: 1 addition & 0 deletions example/simple/web/index.html
Expand Up @@ -43,6 +43,7 @@ <h2>Pirate badge1</h2>
<span>Last modified: 30.03.2015</span>
<!-- Values set in 'site.yaml' can be accessed as properties in the '_site' variable -->
by <span>Mike Mitterer</span>
Template: /Volumes/Daten/DevLocal/DevDart/SiteGen/example/simple/html/_templates/default.html
</footer>
<script type="application/dart" src="/main.dart"></script>
<script src="packages/browser/dart.js"></script>
Expand Down
3 changes: 2 additions & 1 deletion example/simple/web/markdown.html
Expand Up @@ -20,7 +20,7 @@
<h2>About SiteGen</h2>
<!-- '_content' contains the contents of the markdown file (minus the yaml variables in the header) -->
<h1>About SiteGen (Content)</h1>
<h3>Subheadline (Content)</h3>
<h3>Subheadline</h3>
<p>3 <code>~</code>'s is the minimum for designating and separating a <a href="http://rhnh.net/2011/01/31/yaml-tutorial">YAML</a> block, but they can be extended longer -- all that matters
is that the tildes (<code>~</code>) are on their own line.
And anything beyond that gets interpreted as <a href="http://daringfireball.net/projects/markdown/syntax">Markdown</a>!</p>
Expand Down Expand Up @@ -65,6 +65,7 @@ <h3>Subheadline (Content)</h3>
<span>Last modified: 3/30/2015</span>
<!-- Values set in 'site.yaml' can be accessed as properties in the '_site' variable -->
by <span>Mike Mitterer</span>
Template: /Volumes/Daten/DevLocal/DevDart/SiteGen/example/simple/html/_templates/default.html
</footer>
<script type="application/dart" src="/main.dart"></script>
<script src="packages/browser/dart.js"></script>
Expand Down
44 changes: 44 additions & 0 deletions example/simple/web/xmen.html
@@ -0,0 +1,44 @@
<!-- Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
for details. All rights reserved. Use of this source code is governed by a
BSD-style license that can be found in the LICENSE file.
-->
<!DOCTYPE html>
<html>
<head>
<title>X-Men | HudriWudri</title>
<meta charset="utf-8">
<link rel="stylesheet" href="/styles/main.css">
</head>
<body>
<div class="menu">
<a href="/index.html">Home</a> |
<a href="/markdown.html">Markdown</a> |
<a href="/xtreme.html">XTreme simple</a> |
<a href="/about/index.html">About</a>
</div>
<!-- 'title' defaults to markdown filename if not defined in the file's YAML block -->
<h2>X-Men</h2>
<!-- '_content' contains the contents of the markdown file (minus the yaml variables in the header) -->
<ul>
<li>Professor X</li>
<li>Beast</li>
<li>Colossus</li>
<li>Cyclops</li>
<li>Iceman</li>
<li>Marvel Girl</li>
<li>Storm</li>
</ul>
<footer>
<!--
'_date' stores the markdown page's last modified date. The date formatting can be set in 'site.yaml' via the
'date_format' variable.
-->
<span>Last modified: 14.04.2015</span>
<!-- Values set in 'site.yaml' can be accessed as properties in the '_site' variable -->
by <span>Mike Mitterer</span>
Template: /Volumes/Daten/DevLocal/DevDart/SiteGen/example/simple/html/_templates/default.html
</footer>
<script type="application/dart" src="/main.dart"></script>
<script src="packages/browser/dart.js"></script>
</body>
</html>
1 change: 1 addition & 0 deletions example/simple/web/xtreme.html
Expand Up @@ -28,6 +28,7 @@ <h2>xtreme</h2>
<span>Last modified: 30.03.2015</span>
<!-- Values set in 'site.yaml' can be accessed as properties in the '_site' variable -->
by <span>Mike Mitterer</span>
Template: /Volumes/Daten/DevLocal/DevDart/SiteGen/example/simple/html/_templates/default.html
</footer>
<script type="application/dart" src="/main.dart"></script>
<script src="packages/browser/dart.js"></script>
Expand Down
6 changes: 6 additions & 0 deletions lib/src/Config.dart
Expand Up @@ -10,6 +10,7 @@ class Config {
static const _CONF_CONTENT_DIR = 'content_dir';
static const _CONF_TEMPLATE_DIR = 'template_dir';
static const _CONF_OUTPUT_DIR = 'output_dir';
static const _CONF_DATA_DIR = 'data_dir';
static const _CONF_WORKSPACE_DIR = 'workspace';
static const _CONF_DATE_FORMAT = 'date_format';
static const _CONF_YAML_DELIMITER = 'yaml_delimeter';
Expand All @@ -27,6 +28,7 @@ class Config {

_settings[Config._CONF_CONTENT_DIR] = 'html/_content';
_settings[Config._CONF_TEMPLATE_DIR] = 'html/_templates';
_settings[Config._CONF_DATA_DIR] = 'html/_data';
_settings[Config._CONF_OUTPUT_DIR] = 'web';
_settings[Config._CONF_WORKSPACE_DIR] = '.';
_settings[Config._CONF_DATE_FORMAT] = 'dd.MM.yyyy';
Expand Down Expand Up @@ -57,6 +59,8 @@ class Config {

String get outputfolder => _settings[Config._CONF_OUTPUT_DIR];

String get datafolder => _settings[Config._CONF_DATA_DIR];

String get workspace => _settings[Config._CONF_WORKSPACE_DIR];

String get dateformat => _settings[Config._CONF_DATE_FORMAT];
Expand All @@ -80,6 +84,8 @@ class Config {

settings["Content folder"] = contentfolder;
settings["Template folder"] = templatefolder;
settings["Data folder"] = datafolder;

settings["Default template"] = defaulttemplate;
settings["Output folder"] = outputfolder;
settings["Workspace"] = workspace;
Expand Down
40 changes: 40 additions & 0 deletions lib/src/Generator.dart
Expand Up @@ -26,6 +26,7 @@ class Generator {
final Directory contentDir = new Directory(path.absolute(config.contentfolder));
final Directory templateDir = new Directory(path.absolute(config.templatefolder));
final Directory outputDir = new Directory(path.absolute( config.outputfolder));
final Directory dataDir = new Directory(path.absolute( config.datafolder));

Validate.isTrue(contentDir.existsSync(),"ContentDir ${contentDir.path} must exist!");
Validate.isTrue(templateDir.existsSync(),"Templatefolder ${templateDir.path} must exist!");
Expand All @@ -34,6 +35,9 @@ class Generator {
// TODO: support directory hierarchies for markdown, templates and output
final List<File> files = _listContentFilesIn(contentDir);
final List<File> templates = _listTemplatesIn(templateDir);
final List<File> dataFiles = dataDir.existsSync() ? _listDataFilesIn(dataDir) : new List<File>();

final Map dataMap = _getDataMap(dataFiles);

_logger.info("Generating .html files...");
for (final File file in files) {
Expand All @@ -60,9 +64,12 @@ class Generator {

pageOptions = _fillInPageNestingLevel(relativeFileName,pageOptions);
pageOptions = _fillInDefaultPageOptions(config.dateformat,file, pageOptions,config.siteoptions);
pageOptions['_data'] = dataMap;
pageOptions['_content'] = renderTemplate(lines.join('\n'), pageOptions);
pageOptions['_template'] = "none";

_logger.info(pageOptions);

String outputExtension = extension;
if (isMarkdown(file) && _isMarkdownSupported(config.usemarkdown, pageOptions)) {
pageOptions['_content'] = md.markdownToHtml(pageOptions['_content']);
Expand Down Expand Up @@ -126,6 +133,16 @@ class Generator {
return templateDir.listSync().where((file) => file is File).toList();
}

List<File> _listDataFilesIn(final Directory contentDir) {
return contentDir.listSync(recursive: true)
.where((file) => file is File && (

file.path.endsWith('.yaml') ||
file.path.endsWith(".json")

)).toList();
}

bool _isMarkdownSupported(final bool markdownForSite, final Map page_options) {
return markdownForSite || ( page_options.containsKey('markdown_templating') && page_options['markdown_templating'] );
}
Expand Down Expand Up @@ -199,6 +216,29 @@ class Generator {
return pageOptions;
}

Map _getDataMap(final List<File> dataFiles) {
final Map<String,dynamic> dataMap = new Map<String,dynamic>();

dataFiles.forEach((final File file) {
if(file.existsSync()) {
var data;
if(path.extension(file.path) == ".yaml") {

data = yaml.loadYaml(file.readAsStringSync());

} else {

data = JSON.decode(file.readAsStringSync());
}

final String filename = path.basenameWithoutExtension(file.path).toLowerCase();
dataMap[filename] = data;
}
});

return dataMap;
}

/**
* Sample: <link rel="stylesheet" href="{{_page.relative_to_root}}/styles/main.css">
* produces <link rel="stylesheet" href="../styles/main.css"> for about/index.html
Expand Down

0 comments on commit 8beec7a

Please sign in to comment.