Skip to content

Commit

Permalink
Add support for static pages
Browse files Browse the repository at this point in the history
  • Loading branch information
tomraithel committed Jan 24, 2017
1 parent 5c5f90d commit 0252c9e
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 104 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"create": "npm run clean && babel-node ./scripts/create.js",
"build": "npm run clean && babel-node ./scripts/build.js",
"clean": "babel-node ./scripts/clean.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down
11 changes: 0 additions & 11 deletions radar/2016-02-02/languages-and-frameworks/vue.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,3 @@ Hier steht der Rest!

History
-------

{% include
history-entry.html
ring="trial"
date="2017-12-10"
content="This is a history entry

- Foo
- Bar
"
%}
21 changes: 21 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {
createRadar,
outputRadar,
} from './radar';
import {
createStatic,
} from './static';


(async () => {
try {
const radar = await createRadar();
outputRadar(radar);
createStatic(radar);

console.log('Radar build!');
console.log(JSON.stringify(radar, null, 2));
} catch(e) {
console.error('error:', e);
}
})()
51 changes: 0 additions & 51 deletions scripts/create.js

This file was deleted.

45 changes: 44 additions & 1 deletion scripts/file.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,56 @@
import path from 'path';
import { walk } from 'walk';

export const relativePath = (...relativePath) => (
path.resolve(__dirname, '..', ...relativePath)
);

export const srcPath = (...pathInSrc) => (
export const radarPath = (...pathInSrc) => (
relativePath('radar', ...pathInSrc)
);

export const staticPath = (...pathInSrc) => (
relativePath('static-pages', ...pathInSrc)
);

export const distPath = (...pathInDist) => (
relativePath('dist', ...pathInDist)
);

export const getAllMarkdownFiles = (folder) => (
getAllFiles(folder, isMarkdownFile)
);

export const getAllPugFiles = (folder) => (
getAllFiles(folder, isPugFile)
);

const getAllFiles = (folder, predicate) => (
new Promise((resolve, reject) => {
const walker = walk(folder, { followLinks: false });
const files = [];

walker.on("file", (root, fileStat, next) => {
if (predicate(fileStat.name)) {
files.push(path.resolve(root, fileStat.name));
}
next();
});

walker.on("errors", (root, nodeStatsArray, next) => {
nodeStatsArray.forEach(function (n) {
console.error("[ERROR] " + n.name)
console.error(n.error.message || (n.error.code + ": " + n.error.path));
});
next();
});

walker.on("end", () => {
resolve(files.sort());
});
})
);

const isMarkdownFile = (name) => name.match(/\.md$/);

const isPugFile = (name) => name.match(/\.pug$/);
44 changes: 10 additions & 34 deletions scripts/radar.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,21 @@
import { walk } from 'walk';
import fs, { readFile, outputFile } from 'fs-extra';
import path from 'path';
import frontmatter from 'front-matter';
import marked from 'marked';
import { srcPath, distPath } from './file';
import {
radarPath,
distPath,
getAllMarkdownFiles,
} from './file';
import { item as itemTemplate } from './template';

export const createRadar = async (tree) => {
const fileNames = await getAllMarkdownFiles();
const fileNames = (await getAllMarkdownFiles(radarPath())).reverse();
const revisions = await createRevisionsFromFiles(fileNames);
const quadrants = createQuadrants(revisions);
return quadrants;
};

const getAllMarkdownFiles = () => (
new Promise((resolve, reject) => {
const walker = walk(srcPath(), { followLinks: false });
const files = [];

walker.on("file", (root, fileStat, next) => {
if (isMarkdownFile(fileStat.name)) {
files.push(path.resolve(root, fileStat.name));
}
next();
});

walker.on("errors", (root, nodeStatsArray, next) => {
nodeStatsArray.forEach(function (n) {
console.error("[ERROR] " + n.name)
console.error(n.error.message || (n.error.code + ": " + n.error.path));
});
next();
});

walker.on("end", () => {
resolve(files.sort().reverse());
});
})
);

const isMarkdownFile = (name) => name.match(/\.md$/);

const createRevisionsFromFiles = (fileNames) => (
Promise.all(fileNames.map((fileName) => {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -99,7 +74,6 @@ const addRevisionToItem = (item = {
...rest,
} = revision;
return {
quadrant,
attributes: {
...item.attributes,
...revision.attributes,
Expand All @@ -114,8 +88,10 @@ export const outputRadar = (radar) => {
Object.entries(radar).map(([quadrantName, quadrant]) => (
Object.entries(quadrant).map(([itemName, item]) => (
new Promise((resolve, reject) => {
console.log(JSON.stringify(item, null, 2));
outputFile(distPath(quadrantName, `${itemName}.html`), itemTemplate(item), (err, data) => {
outputFile(distPath(quadrantName, `${itemName}.html`), itemTemplate({
quadrantName,
item,
}), (err, data) => {
if (err) {
reject(err);
} else {
Expand Down
39 changes: 39 additions & 0 deletions scripts/static.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { outputFile } from 'fs-extra';
import pug from 'pug';
import frontmatter from 'front-matter';
import marked from 'marked';
import {
staticPath,
distPath,
getAllPugFiles,
} from './file';

export const createStatic = async (radar) => {
const paths = await getAllPugFiles(staticPath());
const fileNames = getPlainFileNames(paths);
return renderStaticPages(radar, fileNames);
return fileNames;
};

const getPlainFileNames = (paths) => (
paths.map((fileName) => {
const [ nameWithSuffix ] = fileName.split('/').slice(-1);
return nameWithSuffix.substr(0, nameWithSuffix.length - 4);
})
)

const renderStaticPages = (radar, fileNames) => (
Promise.all(fileNames.map((name) => (
new Promise((resolve, reject) => (
outputFile(distPath(`${name}.html`), pug.renderFile(staticPath(`${name}.pug`), {
radar,
}), (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
})
))
)))
);
14 changes: 14 additions & 0 deletions static-pages/index.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
extends ../templates/layout.pug

block vars
- var title='Overview'

block content
ul
each quadrant, quadrantName in radar
li
h4= quadrantName
ul
each radarItem, itemName in quadrant
li
a(href='/' + quadrantName + '/' + itemName + '.html')= radarItem.attributes.title
13 changes: 8 additions & 5 deletions templates/item.pug
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
extends layout.pug

block vars
- var title=item.attributes.title

block content
a(href='/' + quadrant + '.html') #{quadrant}
h1 #{attributes.title}
a(href='/' + quadrantName + '.html') #{quadrantName}
h1 #{item.attributes.title}
= ' '
small #{attributes.ring}
small #{item.attributes.ring}

hr
section
!= revisions[0].body
!= item.revisions[0].body
hr

ul
each revision, index in revisions
each revision, index in item.revisions
if index > 0
li
= revision.version
Expand Down
8 changes: 7 additions & 1 deletion templates/layout.pug
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
block vars
html
head
title #{attributes.title} - AOE Tech Radar
title #{title} - AOE Tech Radar
block scripts
script(src='/jquery.js')

h3 AOE Tech Radar

hr

body
block content

0 comments on commit 0252c9e

Please sign in to comment.