Skip to content

Instantly share code, notes, and snippets.

@thomasjbradley
Last active December 21, 2015 07:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasjbradley/6274285 to your computer and use it in GitHub Desktop.
Save thomasjbradley/6274285 to your computer and use it in GitHub Desktop.
Convert a series of Markdown files for a novel into a manuscript using PDFCrowd, MultiMarkdown, and YAML.
<!DOCTYPE html>
<html lang="en-ca">
<head>
<meta charset="utf-8">
<title>{{header}}</title>
<style>
body {
margin: 0;
padding: 0;
background-color: #fff;
color: #000;
font: normal 12pt/2.15 Courier,monospace;
}
h1 {
margin: 0 0 2.15em;
padding: 12.9em 0 0; /* line-height × 6 */
font-size: 1em;
font-weight: normal;
line-height: inherit;
text-align: center;
page-break-before: always;
}
h1 span {
display: block;
}
p {
margin: 0;
padding: 0;
text-indent: 3em;
}
h1 + p {
text-indent: 0;
}
em {
border-bottom: 0.1em solid #000;
font-style: normal;
}
i {
font-style: normal;
}
hr {
margin: 0;
padding: 0;
border: 0;
}
hr:after {
content: "# # #";
display: block;
width: 100%;
text-align: center;
}
footer {
padding-top: 2.15em;
text-align: center;
}
header {
display: block;
overflow: hidden;
}
header i {
float: left;
}
header span {
float: right;
}
article {
display: block;
}
article header {
text-align: center;
}
article header h1 {
margin: 0;
text-transform: uppercase;
}
article header p {
margin-bottom: 2.15em;
text-indent: 0;
}
</style>
</head>
<body>
<article>
<header>
<h1>{{title}}</h1>
<p>By {{author}}</p>
<p>Approx. {{words}} Words</p>
</header>
{{content}}
<footer>END</footer>
</artcile>
</body>
</html>
#!/usr/bin/env coffee
fs = require 'fs'
http = require 'http'
exec = require('child_process').exec
yaml = require('js-yaml')
pdfCrowd = require 'pdfcrowd'
inputDir = __dirname + '/Story/'
outputHtmlFile = 'manuscript.tmp.html'
outputPdfFile = 'Manuscript.pdf'
manifest = yaml.safeLoad(fs.readFileSync(inputDir + 'Manifest.yaml', {encoding:'utf-8'}))
pdfClient = new pdfCrowd.Pdfcrowd('PDFCROWD-USER', 'PDFCROWD-API-KEY')
totalFiles = manifest.contents.length
currentFile = 0
chapterPieces = {}
templatePath = '/thomasjbradley/6274285/raw/e2141a661188de2f6b0020c8a321377010d79f48/template.html'
template = ''
header = manifest.title.short.toUpperCase() + '/' + manifest.author.short.toUpperCase()
renderPdf = () ->
callbacks = pdfCrowd.saveToFile(outputPdfFile)
callbacks.end = () ->
fs.unlinkSync(outputHtmlFile)
pdfClient.convertFile(outputHtmlFile, callbacks, {
width: '8.5in'
height: '11in'
margin_top: '1in'
margin_right: '1in'
margin_bottom: '1in'
margin_left: '1in'
header_html: '<header style="display:block;padding-top:0.5in;font:normal 12pt/2.15 Courier,monospace;overflow:hidden;"><i style="float:left;font-style:normal;">' + header + '</i><span style="float:right;">%p</span></header>'
header_footer_page_exclude_list: 1
use_print_media: true
pdf_scaling_factor: 1
page_numbering_offset: 1
})
renderHtml = () ->
story = ''
manifest.contents.forEach (item, index, arr) ->
story += chapterPieces[item]
output = template
.replace(/\{\{header\}\}/, header)
.replace(/\{\{title\}\}/, manifest.title.full)
.replace(/\{\{author\}\}/, manifest.author.full)
.replace(/\{\{words\}\}/, manifest.words)
.replace(/\{\{content\}\}/, story)
fs.writeFileSync(outputHtmlFile, output, {encoding:'utf-8'})
renderPdf()
addToContent = (file, content) ->
currentFile++
text = content.replace(/\<h1\>([\w\s]+?)\:\s+([\w\s]+?)\<\/h1\>/i, '<h1>$1: <span>$2</span></h1>')
chapterPieces[file] = text
renderHtml() if currentFile == totalFiles
http.get {host: 'gist.github.com', path: templatePath}, (res) ->
res.setEncoding('utf8')
res.on 'data', (chunk) ->
template += chunk
res.on 'end', () ->
manifest.contents.forEach (item, index, arr) ->
exec 'multimarkdown --nolabels --nosmart --nonotes --nomask "' + inputDir + item + '"', (err, stdout, stderr) ->
return if err
addToContent(item, stdout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment