Skip to content

Commit

Permalink
[Delivers #52023891]
Browse files Browse the repository at this point in the history
* codelistings-like-asides-52023891:
  Handle codelistings like asides
  • Loading branch information
mhartl committed Jun 20, 2013
2 parents 74e1a80 + c903636 commit 8728102
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
35 changes: 22 additions & 13 deletions lib/polytexnic-core/postprocessors/html.rb
Expand Up @@ -348,20 +348,29 @@ def subsection(doc)

# Processes codelisting environments.
def codelistings(doc)
doc.xpath('//p[@type="codelisting"]').each do |node|
node.name = 'div'
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'
clean_node node, 'type'
heading, description = node.children[0..1]
listing = Nokogiri::HTML.fragment <<-EOS
<div class="listing">
<span class="header">#{heading.content}.</span>
<span class="description">#{description.content}</span>
</div>
EOS
heading.remove
description.remove
node.children.first.add_previous_sibling listing

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 += '.'

code = heading.at_css('div.code')
node.add_child(code)
end
end

Expand Down
10 changes: 7 additions & 3 deletions lib/polytexnic-core/preprocessors/html.rb
Expand Up @@ -93,11 +93,15 @@ def mark_environments(string)
"#{s}\n\\AddAttToCurrent{type}{chapter}"
end

# Mark code listings with a 'codelisting' type.
string.gsub! /\\begin\{codelisting\}/ do |s|
"#{s}\n\\AddAttToCurrent{type}{codelisting}"
# Wrap codelistings in a 'codelisting' element.
string.gsub! /\\begin{codelisting}/ do |s|
"\\begin{xmlelement*}{codelisting}\n#{s}"
end
string.gsub! /\\end{codelisting}/ do |s|
"#{s}\n\\end{xmlelement*}"
end

# Wrap asides in an 'aside' element.
string.gsub! /\\begin{aside}/ do |s|
"\\begin{xmlelement*}{aside}\n#{s}"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/polytexnic-core/utils.rb
Expand Up @@ -81,7 +81,7 @@ def horrible_backslash_kludge(string)

# Returns true if we are debugging, false otherwise
def debug?
true
false
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/to_html/code_listings_spec.rb
Expand Up @@ -11,7 +11,7 @@
\chapter{Foo bar}
\begin{codelisting}
Creating a gem configuration file.
\heading{Creating a gem configuration file.}
\label{code:create_gemrc}
%= lang:console
\begin{code}
Expand All @@ -26,7 +26,7 @@
it do
should resemble <<-'EOS'
<div id="cid1" data-tralics-id="cid1" class="chapter" data-number="1"><h3><a href="#cid1" class="heading"><span class="number">1 </span>Foo bar</a></h3>
<div id="code-create_gemrc" data-tralics-id="uid1" class="codelisting" data-number="1.1">
<div class="codelisting" id="code-create_gemrc" data-tralics-id="uid1" data-number="1.1">
<div class="heading">
<span class="number">Listing 1.1.</span>
<span class="description">Creating a gem configuration file.</span>
Expand Down

0 comments on commit 8728102

Please sign in to comment.