diff --git a/Guardfile b/Guardfile index 9c11770..14fcda1 100644 --- a/Guardfile +++ b/Guardfile @@ -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 diff --git a/lib/polytexnic-core/postprocessors/html.rb b/lib/polytexnic-core/postprocessors/html.rb index 37af3be..fd10dca 100644 --- a/lib/polytexnic-core/postprocessors/html.rb +++ b/lib/polytexnic-core/postprocessors/html.rb @@ -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) @@ -346,29 +347,43 @@ 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 @@ -376,26 +391,8 @@ def codelistings(doc) # 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 diff --git a/spec/to_html/code_listings_spec.rb b/spec/to_html/codelistings_spec.rb similarity index 100% rename from spec/to_html/code_listings_spec.rb rename to spec/to_html/codelistings_spec.rb