From a62f907aa14f941579ea308ae4019e3c52638fd8 Mon Sep 17 00:00:00 2001 From: Bucko Date: Mon, 2 Oct 2017 14:37:26 -0700 Subject: [PATCH] label templates and split out converter code into utilities --- guide-generator.js | 85 +---- page-templates/guides-archive.html | 484 +++++++++++++++++++++++++++++ page-templates/guides-sidebar.html | 5 +- page-templates/guides-wrapper.html | 12 +- page-templates/page-wrapper.html | 5 +- sidebar-generator.js | 42 +++ utils/createHTML.js | 101 ++++++ 7 files changed, 648 insertions(+), 86 deletions(-) create mode 100644 page-templates/guides-archive.html create mode 100644 sidebar-generator.js create mode 100644 utils/createHTML.js diff --git a/guide-generator.js b/guide-generator.js index f5c0990..570f343 100644 --- a/guide-generator.js +++ b/guide-generator.js @@ -1,8 +1,8 @@ const marked = require('marked'); const fs = require('fs'); const path = require('path'); -const Prism = require('prismjs'); -const PrismLanguages = require('prism-languages'); + +const createHTML = require('./utils/createHTML.js'); const guidesDir = path.resolve(__dirname, 'guides'); const markdownDir = path.resolve(__dirname, 'guides-markdown'); @@ -23,6 +23,7 @@ args.forEach(arg => { } }); +// Information on how to use the command. Retrieved with `--help` arg if ( args.indexOf('--help') > -1 || args.length === 0 ) { console.log('Build guides from markdown files. Available options are:'); console.log('\'--author\' e.g. `--author="JOHN SNOW"`'); @@ -42,80 +43,6 @@ if ( args.indexOf('--help') > -1 || args.length === 0 ) { return; } - -/****** -Prepare the marked renderer -******/ -const renderer = new marked.Renderer(); - - -// Custom renderer for code snippet highlighting -const getPostMeta = (author='bcoin-org') => ''; - - -// Custom renderer for top two level headers -renderer.heading = (text, level) => { - if (level == '2' || level == '1' ) { - let header = '

' - + text + '

'; - - if (author) { - postMeta = getPostMeta(author) - header += postMeta; - } - - return header; - } else { - return `${text}`; - } -} - -renderer.code = function (code, language) { - if (language === 'post-author') { - // only return code block if wasn't set by argument - return postMeta ? '' : getPostMeta(code); - } - - return `
`
-           + ``
-           + Prism.highlight(code, PrismLanguages[language])
-           + '
'; -} - -marked.setOptions({ - renderer, - gfm: true, -}); - - -const createHTML = markdownFile => { - - const markdownString = fs.readFileSync(markdownFile, 'utf8'); - - // Assemble guide text container - let blogText = marked(markdownString); - - // Get the guide html template and find start of guide section - const template = fs.readFileSync(path.resolve(markdownDir, 'guides-template.txt')) - .toString().split('\n'); - const startText = 'START OF GUIDE'; // NOTE: Make sure to change this if the comment text changes - let startLine = 0; - - for (let i=0; i <= template.length; i++) { - if (template[i].indexOf(startText) > -1) { - startLine = i + 1; - break; - } - } - - template.splice(startLine, 0, blogText); - - fs.writeFileSync(htmlFile, template.join('\n')); - console.log(`Finished ${path.basename(htmlFile)}`); -} - let markdownFile; let htmlFile; if (all) { @@ -129,7 +56,7 @@ if (all) { console.log('Starting file conversion: ', file); markdownFile = path.resolve(markdownDir, file); htmlFile = path.resolve(guidesDir, file.replace(/\.[^/.]+$/, ".html")); - createHTML(markdownFile); + createHTML(markdownFile, markdownDir, htmlFile); } } console.log('All files done!'); @@ -137,7 +64,7 @@ if (all) { } else { markdownFile = path.resolve(markdownDir, file); htmlFile = path.resolve(guidesDir, file.replace(/\.[^/.]+$/, ".html")); - createHTML(markdownFile); + createHTML(markdownFile, markdownDir, htmlFile, author, postMeta); } /** Notes for building out page template: @@ -145,7 +72,7 @@ if (all) { - header (includes main nav) - guides-wrapper (main content wrapper) - guides-sidebar - - guides-wrapper + (insert guide content) - footer - footer-scripts **/ \ No newline at end of file diff --git a/page-templates/guides-archive.html b/page-templates/guides-archive.html new file mode 100644 index 0000000..ce6671d --- /dev/null +++ b/page-templates/guides-archive.html @@ -0,0 +1,484 @@ + + + + + + + + + bcoin | Extending Bitcoin into Enterprise & Production + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
Loading... + + + + + + + + + + + + + + + + + + + + + + +
+
+ + + +
+
+ + + + + + + +
+ +
+ + + + +
+
+ + + +
+ + +
+
+ +
+
+ +

 Guides  and  Videos 

+ +
+
+ +
+
+ + + +
+
+ +
+ + + + + + +
+
+
OS Installs
+ + + + + +
+
Guides
+ + +
+
+
+ +
+
+

Generate a Bitcoin Address

+ +

Follow along with the steps to build a transaction from scratch using built-in bcoin utilities. These steps are based on those outlined in the Bitcoin Wiki.

+ Start › +
+
+
+
+ + +
+
+
+ +
+
+

Intro to Scripting

+ +

Scripts are array-like objects with some helper functions. Learn the basics here.

+ Start › +
+
+
+
+ + +
+
+
+ +
+
+

Create an OP_RETURN

+ +

This example creates a transaction with output containing a null data script with our data.

+ Start › +
+
+
+
+ + +
+ + +
+ +
+
+ + +
+ + + + + + +
+
+ +
+
+
+

Ready to start building? Read the docs!

+ Documentation +
+
+
+ +
+
+ + + + + + + + + + + + + + + + + + + + + +s \ No newline at end of file diff --git a/page-templates/guides-sidebar.html b/page-templates/guides-sidebar.html index 502401b..e687418 100644 --- a/page-templates/guides-sidebar.html +++ b/page-templates/guides-sidebar.html @@ -3,18 +3,21 @@
Install

Guides
diff --git a/page-templates/guides-wrapper.html b/page-templates/guides-wrapper.html index a01a45f..0c22e70 100644 --- a/page-templates/guides-wrapper.html +++ b/page-templates/guides-wrapper.html @@ -23,8 +23,8 @@
- - + +
@@ -86,8 +86,10 @@ - - + +
- \ No newline at end of file + + + \ No newline at end of file diff --git a/page-templates/page-wrapper.html b/page-templates/page-wrapper.html index 66267ac..8ecfef4 100644 --- a/page-templates/page-wrapper.html +++ b/page-templates/page-wrapper.html @@ -66,6 +66,9 @@ - + + + + diff --git a/sidebar-generator.js b/sidebar-generator.js new file mode 100644 index 0000000..fcc1786 --- /dev/null +++ b/sidebar-generator.js @@ -0,0 +1,42 @@ +const marked = require('marked'); +const fs = require('fs'); +const path = require('path'); + +const getGuideTitles = function getGuideTitles() { + const markdownDir = path.resolve(__dirname, 'guides-markdown'); + const titles = []; + const renderer = new marked.Renderer(); + + // Add text to list of titles if in a header level 1 or 2 + renderer.heading = (text, level) => { + if (level == '2' || level == '1' ) { + titles.push(text); + } + } + + marked.setOptions({ + renderer, + gfm: true, + }); + + // Promisify the readdir so we can retrieve the titles + // when it's done + return new Promise((resolve, reject) => { + fs.readdir(markdownDir, (err, files) => { + if (err) reject(err); + for (let i=0; i < files.length; i++) { + const file = files[i]; + const ext = path.extname(file); + if (ext === '.md') { + markdownFile = path.resolve(markdownDir, file); + const markdownString = fs.readFileSync(markdownFile, 'utf8'); + marked(markdownString); + } + } + resolve(titles); + }); + }); +} + +getGuideTitles().then(titles => console.log(titles)); + diff --git a/utils/createHTML.js b/utils/createHTML.js new file mode 100644 index 0000000..34b2358 --- /dev/null +++ b/utils/createHTML.js @@ -0,0 +1,101 @@ +const marked = require('marked'); +const fs = require('fs'); +const path = require('path'); +const Prism = require('prismjs'); +const PrismLanguages = require('prism-languages'); + +const createHTML = async function createHTML(markdownFile, markdownDir, htmlFile, author, postMeta) { + /****** + Prepare the marked renderer + ******/ + + const guidesDir = path.resolve(__dirname, 'guides'); + const templatesDir = path.resolve(__dirname, 'page-templates'); + + const renderer = new marked.Renderer(); + let guideTitle, guideDescription; + + // Custom renderer for code snippet highlighting + const getPostMeta = (author='bcoin-org') => ''; + + + // Custom renderer for top two level headers + renderer.heading = (text, level) => { + if (level == '2' || level == '1' ) { + let header = '

' + + text + '

'; + + if (author) { + postMeta = getPostMeta(author) + header += postMeta; + } + guideTitle = text; + return header; + } else { + return `${text}`; + } + } + + renderer.code = function (code, language) { + if (language === 'post-author') { + // only return code block if wasn't set by argument + return postMeta ? '' : getPostMeta(code); + } + + if (language === 'post-description') { + guideDescription = code; + return; + } + + return `
`
+             + ``
+             + Prism.highlight(code, PrismLanguages[language])
+             + '
'; + } + + marked.setOptions({ + renderer, + gfm: true, + }); + + const markdownString = fs.readFileSync(markdownFile, 'utf8'); + + // Assemble guide text container + let blogText = marked(markdownString); + + // Get the guide html template and find start of guide section + // const pageWrapper = fs.readFileSync(path.resolve(templatesDir), 'page-wrapper.html').toString().split('\n'); + // const header = fs.readFileSync(path.resolve(templatesDir), 'header.html').toString().split('\n'); + // const guidesSidebar = fs.readFileSync(path.resolve(templatesDir), 'guides-sidebar.html').toString().split('\n'); + // const guidesWrapper = fs.readFileSync(path.resolve(templatesDir), 'guide-wrapper.html').toString().split('\n'); + // const footer = fs.readFileSync(path.resolve(templatesDir), 'footer.html').toString().split('\n'); + + const template = fs.readFileSync(path.resolve(markdownDir, 'guides-template.txt')) + .toString().split('\n'); + // const startHeaderText = 'HEADER-SECTION'; + // const startGuidesWrapper = 'GUIDES-WRAPPER'; + // const startGuidesSidebar = 'GUIDES-SIDEBAR'; + const startGuideText = 'START OF GUIDE'; // NOTE: Make sure to change this if the comment text changes + // const startFooter = 'FOOTER-SECTION'; + // const startFooterScripts = 'FOOTER-SCRIPTS'; + + let startLine = 0; + + // 1. insert post into guidesWrapper + // 2. insert + + for (let i=0; i <= template.length; i++) { + if (template[i].indexOf(startGuideText) > -1) { + startLine = i + 1; + break; + } + } + + template.splice(startLine, 0, blogText); + fs.writeFileSync(htmlFile, template.join('\n')); + console.log(`Finished ${path.basename(htmlFile)}`); +} + +module.exports = createHTML; \ No newline at end of file