Skip to content

Commit

Permalink
Add build_headings method
Browse files Browse the repository at this point in the history
[#52028803]
  • Loading branch information
mhartl committed Jun 20, 2013
1 parent 8728102 commit 9a79f80
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 36 deletions.
9 changes: 8 additions & 1 deletion Guardfile
Expand Up @@ -3,6 +3,13 @@

guard :rspec do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { "spec"}
watch(%r{^lib/(.+)\.rb$}) { "spec" }
## Uncomment & edit below (and comment out above) to watch particular specs.
# watch(%r{^lib/(.+)\.rb$}) do
# [
# "spec/to_html/asides_spec.rb",
# "spec/to_html/codelistings_spec.rb"
# ]
# end
watch('spec/spec_helper.rb') { "spec" }
end
67 changes: 32 additions & 35 deletions lib/polytexnic-core/postprocessors/html.rb
Expand Up @@ -24,6 +24,7 @@ def xml_to_html(xml)
set_ids(doc)
chapters_and_section(doc)
subsection(doc)
headings(doc)
codelistings(doc)
asides(doc)
title(doc)
Expand Down Expand Up @@ -346,56 +347,52 @@ def subsection(doc)
end
end

# Processes codelisting environments.
def codelistings(doc)
# Converts heading elements to the proper spans.
# Headings are used in codelisting-like environments such as asides
# and codelistings.
def headings(doc)
doc.xpath('//heading').each do |node|
node.name = 'span'
node['class'] = 'description'
end
doc.xpath('//codelisting').each do |node|
node.name = 'div'
node['class'] = 'codelisting'
end

heading = node.at_css('p')
heading.attributes.each do |key, value|
node.set_attribute(key, value)
heading.remove_attribute(key)
end
heading.name = 'div'
heading['class'] = 'heading'
# Builds the full heading for codelisting-like environments.
# The full heading, such as "Listing 1.1. Foo bars." needs to be
# extracted and manipulated to produce the right tags and classes.
def build_heading(node, css_class)
node.name = 'div'
node['class'] = css_class

number = heading.at_css('strong')
number.name = 'span'
number['class'] = 'number'
number.content += '.'
heading = node.at_css('p')
heading.attributes.each do |key, value|
node.set_attribute(key, value)
heading.remove_attribute(key)
end
heading.name = 'div'
heading['class'] = 'heading'

number = heading.at_css('strong')
number.name = 'span'
number['class'] = 'number'
number.content += '.'

heading
end

# Processes codelisting environments.
def codelistings(doc)
doc.xpath('//codelisting').each do |node|
heading = build_heading(node, 'codelisting')
code = heading.at_css('div.code')
node.add_child(code)
end
end

# Processes boxes/asides.
def asides(doc)
doc.xpath('//heading').each do |node|
node.name = 'span'
node['class'] = 'description'
end
doc.xpath('//aside').each do |node|
node.name = 'div'
node['class'] = 'aside'

heading = node.at_css('p')
heading.attributes.each do |key, value|
node.set_attribute(key, value)
heading.remove_attribute(key)
end
heading.name = 'div'
heading['class'] = 'heading'

number = heading.at_css('strong')
number.name = 'span'
number['class'] = 'number'
number.content += '.'
build_heading(node, 'aside')
end
end

Expand Down
File renamed without changes.

0 comments on commit 9a79f80

Please sign in to comment.