Permalink
Browse files

Add script to generate website

  • Loading branch information...
1 parent cd0d096 commit cb2abf97fc97e5fd1aa7eccd7929e3cc11826560 @devongovett committed Feb 23, 2014
Showing with 149 additions and 5 deletions.
  1. +2 −4 .gitignore
  2. +4 −0 docs/.gitignore
  3. +95 −0 docs/generate_website.coffee
  4. +46 −0 docs/template.jade
  5. +2 −1 package.json
View
@@ -1,10 +1,8 @@
-
lib/font/tables/.DS_Store
-
.DS_Store
-
/node_modules/
/tests/
node-zlib/
src/
-playground/
+playground/
+*.html
View
@@ -0,0 +1,4 @@
+*.html
+css/
+img/
+js/
@@ -0,0 +1,95 @@
+jade = require 'jade'
+markdown = require('markdown').markdown
+fs = require 'fs'
+vm = require 'vm'
+coffee = require 'coffee-script'
+{exec} = require 'child_process'
+PDFDocument = require '../'
+
+files = [
+ '../README.md'
+ 'getting_started.coffee.md'
+ 'vector.coffee.md'
+ 'text.coffee.md'
+ 'images.coffee.md'
+ 'annotations.coffee.md'
+]
+
+# shared lorem ipsum text so we don't need to copy it into every example
+lorem = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam in suscipit purus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vivamus nec hendrerit felis. Morbi aliquam facilisis risus eu lacinia. Sed eu leo in turpis fringilla hendrerit. Ut nec accumsan nisl. Suspendisse rhoncus nisl posuere tortor tempus et dapibus elit porta. Cras leo neque, elementum a rhoncus ut, vestibulum non nibh. Phasellus pretium justo turpis. Etiam vulputate, odio vitae tincidunt ultricies, eros odio dapibus nisi, ut tincidunt lacus arcu eu elit. Aenean velit erat, vehicula eget lacinia ut, dignissim non tellus. Aliquam nec lacus mi, sed vestibulum nunc. Suspendisse potenti. Curabitur vitae sem turpis. Vestibulum sed neque eget dolor dapibus porttitor at sit amet sem. Fusce a turpis lorem. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae;'
+
+extractHeaders = (tree) ->
+ headers = []
+
+ for node, index in tree
+ if node[0] is 'header' and (headers.length is 0 or node[1].level > 1)
+ node[1].level = 2 if node[1].level > 2
+ hash = node[2].toLowerCase().replace(/\s+/g, '_')
+ node[1].id = hash
+ headers.push
+ hash: hash
+ title: node[2]
+
+ return headers
+
+imageIndex = 0
+generateImages = (tree) ->
+ # find code blocks
+ codeBlocks = []
+ for node in tree
+ if node[0] is 'code_block'
+ codeBlocks.push node[1]
+
+ for node in tree
+ if node[0] is 'para' and Array.isArray(node[1]) and node[1][0] is 'img'
+ # compile the code
+ attrs = node[1][1]
+ code = codeBlocks[attrs.alt]
+ code = coffee.compile code if code
+ delete attrs.height # used for pdf generation
+
+ # create a PDF and run the example
+ doc = new PDFDocument
+ doc.translate doc.x, doc.y
+ doc.scale 0.8
+ doc.x = doc.y = 0
+
+ vm.runInNewContext code,
+ doc: doc
+ lorem: lorem
+
+ delete attrs.title
+ delete attrs.alt
+ attrs.href = "img/#{imageIndex}.png"
+
+ # write the PDF, convert to PNG using the mac `sips`
+ # command line tool, and trim with graphicsmagick
+ do (f = "img/#{imageIndex++}") ->
+ doc.write "#{f}.pdf", ->
+ exec "sips -s format png #{f}.pdf --out #{f}.png", ->
+ fs.unlink "#{f}.pdf"
+ exec "gm convert #{f}.png -trim #{f}.png"
+
+pages = []
+for file in files
+ content = fs.readFileSync file, 'utf8'
+ tree = markdown.parse(content)
+ headers = extractHeaders(tree)
+ generateImages(tree)
+
+ file = file
+ .replace(/\.coffee\.md$/, '')
+ .replace(/README\.md/, 'index')
+
+ pages.push
+ file: file
+ url: '/docs/' + file + '.html'
+ title: headers[0].title
+ headers: headers.slice(1)
+ content: markdown.toHTML(tree)
+
+for page, index in pages
+ page.pages = pages
+ page.index = index
+ html = jade.renderFile 'template.jade', page
+ fs.writeFileSync page.file + '.html', html, 'utf8'
View
@@ -0,0 +1,46 @@
+doctype html
+html
+ head
+ meta(charset='utf-8')
+ title= title
+ link(rel='stylesheet', href='http://fonts.googleapis.com/css?family=Source+Code+Pro:400,700|Alegreya:700|Merriweather')
+ link(rel='stylesheet', href='/docs/css/index.css')
+ link(rel='stylesheet', href='/docs/css/github.css')
+ body
+ nav(class='sidebar')
+ ul
+ li
+ a(href='/', class=(/index/.test(url) ? 'selected' : '')) Home
+ li
+ a(href=pages[0].url) Documentation
+ ul
+ each page in pages.slice(1)
+ li
+ a(href=page.url, class=page.title == title ? 'selected' : '')
+ = page.title.replace(/(with|in) PDFKit/, '')
+
+ - if (page.title == title && headers.length)
+ ul
+ each header in headers
+ li
+ a(href='#' + header.hash)= header.title
+
+ li
+ a(href='/docs/guide.pdf') PDF Guide
+ li
+ a(href='/example.pdf') Example PDF
+ li
+ a(href='http://github.com/devongovett/pdfkit') Source Code
+
+ .main
+ != content
+ nav
+ - if (index > 0)
+ a(href=pages[index - 1].url, class='previous') Previous
+
+ - if (index < pages.length - 1)
+ a(href=pages[index + 1].url, class='next') Next
+
+ script(src='https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js')
+ script(src='/docs/js/scroll.js')
+ script(src='/docs/js/highlight.pack.js')
View
@@ -24,7 +24,8 @@
"devDependencies": {
"coffee-script": ">=1.0.1",
"codemirror": "~3.20.0",
- "markdown": "~0.5.0"
+ "markdown": "~0.5.0",
+ "jade": "~1.1.5"
},
"dependencies": {
"png-js": ">=0.1.0",

0 comments on commit cb2abf9

Please sign in to comment.