indentation works; not exactly sure how

This commit is contained in:
Christian Genco 2015-04-06 14:03:20 -05:00
parent 4dfd650dac
commit fc6b1b35cd
3 changed files with 84 additions and 132 deletions

View File

@ -19,67 +19,26 @@ vm.runInNewContext coffeeMode, CodeMirror: CodeMirror
# style definitions for markdown # style definitions for markdown
styles = styles =
# h1: default:
# font: 'Times-Roman'
# fontSize: 25
# padding: 15
# h2:
# font: 'Times-Roman'
# fontSize: 18
# padding: 10
# h3:
# font: 'Times-Roman'
# fontSize: 18
# padding: 10
meta:
font: 'Times-Roman' font: 'Times-Roman'
fontSize: 12 fontSize: 12
lineGap: 24 lineGap: 24
align: 'left' align: 'left'
meta:
indent: 0 indent: 0
title: title:
font: 'Times-Roman'
fontSize: 12
lineGap: 24
align: 'center' align: 'center'
para: para:
font: 'Times-Roman' indent: 72/2
fontSize: 12 blockquote:
lineGap: 24 indent: 0
align: 'left' color: 'red'
indent: 72 font: 'Times-Italic'
# padding: 10 marginLeft: 72
# code:
# font: 'Times-Roman'
# fontSize: 9
# code_block:
# padding: 10
# background: '#2c2c2c'
# inlinecode:
# font: 'Times-Roman'
# fontSize: 10
# listitem:
# font: 'Times-Roman'
# fontSize: 10
# padding: 6
# link:
# font: 'Times-Roman'
# fontSize: 10
# color: 'blue'
# underline: true
# example:
# font: 'Times-Roman'
# fontSize: 9
# color: 'black'
# padding: 10
em: em:
font: 'Times-Italic' font: 'Times-Italic'
align: 'left'
strong: strong:
font: 'Times-Bold' font: 'Times-Bold'
align: 'left'
# u:
# underline: true
# syntax highlighting colors # syntax highlighting colors
# based on Github's theme # based on Github's theme
@ -120,6 +79,7 @@ class Node
@text = tree @text = tree
return return
# console.dir tree
@type = tree.shift() @type = tree.shift()
@attrs = {} @attrs = {}
@ -130,24 +90,26 @@ class Node
@content = while tree.length @content = while tree.length
new Node tree.shift() new Node tree.shift()
# console.log "type =", @type
switch @type switch @type
when 'header' when 'header'
@type = 'h' + @attrs.level @type = 'h' + @attrs.level
when 'code_block' # when 'code_block'
# use code mirror to syntax highlight the code block # # use code mirror to syntax highlight the code block
code = @content[0].text # code = @content[0].text
@content = [] # @content = []
CodeMirror.runMode code, 'coffeescript', (text, style) => # CodeMirror.runMode code, 'coffeescript', (text, style) =>
color = colors[style] or colors.default # color = colors[style] or colors.default
opts = # opts =
color: color # color: color
continued: text isnt '\n' # continued: text isnt '\n'
@content.push new Node ['code', opts, text] # @content.push new Node ['code', opts, text]
@content[@content.length - 1]?.attrs.continued = false # @content[@content.length - 1]?.attrs.continued = false
codeBlocks.push code # codeBlocks.push code
when 'img' when 'img'
# images are used to generate inline example output # images are used to generate inline example output
@ -158,7 +120,9 @@ class Node
@code = coffee.compile code if code @code = coffee.compile code if code
@height = +@attrs.title or 0 @height = +@attrs.title or 0
@style = styles[@type] or styles.para @style = _.extend({}, styles.default, styles[@type])
# @style.continued = @attrs.continued if @attrs.continued?
# console.log @style
# sets the styles on the document for this node # sets the styles on the document for this node
setStyle: (doc) -> setStyle: (doc) ->
@ -168,45 +132,25 @@ class Node
if @style.fontSize if @style.fontSize
doc.fontSize @style.fontSize doc.fontSize @style.fontSize
if @style.color or @attrs.color if @style.color
doc.fillColor @style.color or @attrs.color doc.fillColor @style.color
else else
doc.fillColor 'black' doc.fillColor 'black'
options = {} # options = _.extend({}, @style)
options.lineGap = @style.lineGap || 24 # # options.lineGap = @style.lineGap
options.align = @style.align # # options.align = @style.align
options.indent = @style.indent # # options.indent = @style.indent
# options.paragraphGap = 24 # # options.link = @attrs.href or false # override continued link
options.link = @attrs.href or false # override continued link # options.continued = @attrs.continued if @attrs.continued?
options.continued = @attrs.continued if @attrs.continued? # console.log "options =", options
return options # return options
null
# renders this node and its subnodes to the document # renders this node and its subnodes to the document
render: (doc, continued = false) -> render: (doc, continued = false) ->
console.log "rendering node: ", @
switch @type switch @type
when 'example'
@setStyle doc
# translate all points in the example code to
# the current point in the document
doc.moveDown()
doc.save()
doc.translate(doc.x, doc.y)
x = doc.x
y = doc.y
doc.x = doc.y = 0
# run the example code with the document
vm.runInNewContext @code,
doc: doc
lorem: lorem
# restore points and styles
y += doc.y
doc.restore()
doc.x = x
doc.y = y + @height
when 'hr' when 'hr'
doc.addPage() doc.addPage()
else else
@ -214,25 +158,31 @@ class Node
for fragment, index in @content for fragment, index in @content
if fragment.type is 'text' if fragment.type is 'text'
# add a new page for each heading, unless it follows another heading # add a new page for each heading, unless it follows another heading
if @type in ['h1', 'h2'] and lastType? and lastType isnt 'h1' # if @type in ['h1', 'h2'] and lastType? and lastType isnt 'h1'
doc.addPage() # doc.addPage()
# set styles and whether this fragment is continued (for rich text wrapping) # set styles and whether this fragment is continued (for rich text wrapping)
options = @setStyle doc @setStyle doc
options.continued ?= continued or index < @content.length - 1 # @style.continued ?= continued or index < @content.length - 1
# @style.continued = continued
# remove newlines unless this is code # remove newlines unless this is code
unless @type is 'code' # unless @type is 'code'
fragment.text = fragment.text.replace(/[\r\n]\s*/g, ' ') # fragment.text = fragment.text.replace(/[\r\n]\s*/g, ' ')
doc.text fragment.text, options # console.log "rendering text. continued =", continued, 'attrs.continued =', @attrs.continued
doc.text fragment.text, _.extend({}, @style, {continued: continued or index < @content.length - 1})
else else
console.log "rendering fragment #{fragment.type}"
fragment.render doc, index < @content.length - 1 and @type isnt 'bulletlist' fragment.render doc, index < @content.length - 1 and @type isnt 'bulletlist'
# fragment.render doc, fragment.type == "para"
lastType = @type lastType = @type
if @style.padding if @style.marginTop
doc.y += @style.padding doc.y += @style.marginTop
# if @style.marginLeft
# doc.x += @style.marginLeft
# reads and renders a markdown/literate coffeescript file to the document # reads and renders a markdown/literate coffeescript file to the document
render = (doc, filename) -> render = (doc, filename) ->
@ -259,20 +209,21 @@ render = (doc, filename) ->
value = meta[2] value = meta[2]
metadata[key] = value metadata[key] = value
else else
body += line body += line + "\n"
metadata.lastName ||= metadata.author?.split(" ").last() metadata.lastName ||= metadata.author?.split(" ").last()
# console.log metadata # console.log metadata
# add header # add header
doc.text(metadata.author, styles.meta) doc.text(metadata.author, _.extend({}, styles.default, styles.meta))
doc.text(metadata.instructor, styles.meta) doc.text(metadata.instructor, _.extend({}, styles.default, styles.meta))
doc.text(metadata.course, styles.meta) doc.text(metadata.course, _.extend({}, styles.default, styles.meta))
doc.text(metadata.date, styles.meta) doc.text(metadata.date, _.extend({}, styles.default, styles.meta))
doc.text(metadata.title, styles.title) doc.text(metadata.title, _.extend({}, styles.default, styles.title))
tree = md.parse body tree = md.parse body
tree.shift() console.log tree
tree.shift() # ignore 'markdown' first element
while tree.length while tree.length
node = new Node tree.shift() node = new Node tree.shift()
@ -292,30 +243,28 @@ render = (doc, filename) ->
doc doc
# renders the title page of the guide # renders the title page of the guide
renderTitlePage = (doc) -> # renderTitlePage = (doc) ->
title = 'PDFKit Guide' # title = 'PDFKit Guide'
author = 'By Devon Govett' # author = 'By Devon Govett'
version = 'Version ' + require('./package.json').version # version = 'Version ' + require('./package.json').version
doc.font 'fonts/AlegreyaSans-Light.ttf', 60 # doc.font 'fonts/AlegreyaSans-Light.ttf', 60
doc.y = doc.page.height / 2 - doc.currentLineHeight() # doc.y = doc.page.height / 2 - doc.currentLineHeight()
doc.text title, align: 'center' # doc.text title, align: 'center'
w = doc.widthOfString(title) # w = doc.widthOfString(title)
doc.fontSize 20 # doc.fontSize 20
doc.y -= 10 # doc.y -= 10
doc.text author, # doc.text author,
align: 'center' # align: 'center'
indent: w - doc.widthOfString(author) # indent: w - doc.widthOfString(author)
doc.font styles.para.font, 10 # doc.font styles.para.font, 10
doc.text version, # doc.text version,
align: 'center' # align: 'center'
indent: w - doc.widthOfString(version) # indent: w - doc.widthOfString(version)
doc.addPage() # doc.addPage()
renderHeader = (doc) ->
do -> do ->
doc = new PDFDocument doc = new PDFDocument

BIN
guide.pdf

Binary file not shown.

View File

@ -4,10 +4,13 @@ course: English 624
date: 12 February 2012 date: 12 February 2012
title: Toward a Recovery of Nineteenth Century Farming Handbooks title: Toward a Recovery of Nineteenth Century Farming Handbooks
While researching texts like *Harry Potter* written about nineteenth century farming, I found a few authors who published books about the literature of nineteenth century farming, particularly agricultural journals, newspapers, pamphlets, and brochures. These authors often placed the farming literature they were studying into an historical context by discussing the important events in agriculture of the year in which the literature was published (see Demaree, for example). However, while these authors discuss journals, newspapers, pamphlets, and brochures, I could not find much discussion about another important source of farming knowledge: farming handbooks. My goal in this paper is to bring this source into the agricultural literature discussion by connecting three agricultural handbooks from the nineteenth century with nineteenth century agricultural history. While researching texts like *Harry Potter* and *Curious George* written about **nineteenth century farming**, I found a few authors who published books about the literature of nineteenth century farming, particularly agricultural journals, newspapers, pamphlets, and brochures. These authors often placed the farming literature they were studying into an historical context by discussing the important events in agriculture of the year in which the literature was published (see Demaree, for example). However, while these authors discuss journals, newspapers, pamphlets, and brochures, I could not find much discussion about another important source of farming knowledge: farming handbooks. My goal in this paper is to bring this source into the agricultural literature discussion by connecting three agricultural handbooks from the nineteenth century with nineteenth century agricultural history.
To achieve this goal, I have organized my paper into four main sections, two of which have sub-sections. In the first section, I provide an account of three important events in nineteenth century agricultural history: population and technological changes, the distribution of scientific new knowledge, and farming's influence on education. In the second section, I discuss three nineteenth century farming handbooks in connection with the important events described in the first section. I end my paper with a third section that offers research questions that could be answered in future versions of this paper and conclude with a fourth section that discusses the importance of expanding this particular project. I also include an appendix after the Works Cited that contains images of the three handbooks I examined. Before I can begin the examination of the three handbooks, however, I need to provide an historical context in which the books were written, and it is to this that I now turn. To achieve this goal, I have organized my paper into four main sections, two of which have sub-sections. In the first section, I provide an account of three important events in nineteenth century agricultural history: population and technological changes, the distribution of scientific new knowledge, and farming's influence on education. In the second section, I discuss three nineteenth century farming handbooks in connection with the important events described in the first section. I end my paper with a third section that offers research questions that could be answered in future versions of this paper and conclude with a fourth section that discusses the importance of expanding this particular project. I also include an appendix after the Works Cited that contains images of the three handbooks I examined. Before I can begin the examination of the three handbooks, however, I need to provide an historical context in which the books were written, and it is to this that I now turn.
By the 1860s, the need for this knowledge was strong enough to affect education. John Nicholson anticipated this effect in 1820 in the “Experiments” section of his book *The Farmers Assistant; Being a Digest of All That Relates to Agriculture and the Conducting of Rural Affairs; Alphabetically Arranged and Adapted for the United States*: By the 1860s, the need for this knowledge was strong enough to affect education. John Nicholson anticipated this effect in 1820 in the "Experiments" section of his book *The Farmers Assistant; Being a Digest of All That Relates to Agriculture and the Conducting of Rural Affairs; Alphabetically Arranged and Adapted for the United States*:
> Perhaps it would be well, if some institution were devised, and supported at the expense of the State, which would be so organized as would tend most effectually to produce a due degree of emulation among Farmers, by rewards and honorary distinctions conferred by those who, by their successful experimental efforts and improvements, should render themselves duly entitled to them. (92) > Perhaps it would be well, if some institution were devised, and supported at the expense of the State, which would be so organized as would tend most effectually to produce a due degree of emulation among Farmers, by rewards and honorary distinctions conferred by those who, by their successful experimental efforts and improvements, should render themselves duly entitled to them. (92)
Here's my final paragraph.