Skip to content
This repository has been archived by the owner on Oct 20, 2020. It is now read-only.

Commit

Permalink
render references and appendix
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Aug 28, 2011
1 parent 2c742d9 commit 24ca24f
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 deletions.
69 changes: 66 additions & 3 deletions rfc.rb
Expand Up @@ -131,12 +131,10 @@ def elements
end

class Xref < NodeWrapper
# todo: smart text insertion
def text
super.presence || target
super.presence || document.lookup_anchor(target) || target
end

# todo: proper linkage (e.g. "refs.RFC2223")
def target
self['target']
end
Expand Down Expand Up @@ -247,6 +245,36 @@ def unindent(text)
end
end

class Reference < NodeWrapper
def id?
self['anchor'].present?
end

def id
self['anchor'].parameterize
end

def title
text_at 'title'
end

def url
text_at './@target'
end

def month
text_at './/date/@month'
end

def year
text_at './/date/@year'
end

def series
all('./seriesInfo').map {|s| "#{s['name']} #{s['value']}" }
end
end

class Document < NodeWrapper
def initialize(from)
super Nokogiri::XML(from)
Expand All @@ -273,6 +301,17 @@ def sections
all('./middle/section').map {|node| wrap(node, Section) }
end

def back_sections
all('./back/section').map {|node|
wrap(node, Section) { |s|
s.classnames << 'back'
# indent them one level deeper since we're wrapping them
# in an additional <section> element manually
s.level = 3
}
}
end

def month
text_at './front/date/@month'
end
Expand All @@ -291,6 +330,30 @@ def abstract
def keywords
all('./front/keyword/text()').map(&:text)
end

def anchor_map
all('.//*[@anchor]').each_with_object({}) do |node, map|
map[node['anchor']] = node
end
end

def lookup_anchor(name)
if node = anchor_map[name]
if 'reference' == node.node_name
if series = node.at('./seriesInfo[@name="RFC"]')
"RFC #{series['value']}"
elsif title = node.at('.//title')
title.text
end
else
node['title']
end
end
end

def references
all('./back/references/reference').map {|node| wrap(node, Reference) }
end
end

module Helpers
Expand Down
18 changes: 18 additions & 0 deletions templates/document.erb
Expand Up @@ -27,4 +27,22 @@
<% for section in sections %>
<%= render section %>
<% end %>
<% if references? %>
<section class="references">
<h2>References</h2>
<ol><% for reference in references %>
<%= render reference %>
<% end %></ol>
</section>
<% end %>
<% if back_sections? %>
<section class="appendix">
<h2>Appendix</h2>
<% for section in back_sections %>
<%= render section %>
<% end %>
</section>
<% end %>
</article>
5 changes: 5 additions & 0 deletions templates/reference.erb
@@ -0,0 +1,5 @@
<li<%= id_attribute %>>
<%= link_to title, url %>
<% if series? %>(<%== series.join(', ') %>)<% end %><% if year? %>,
<%== month %> <%== year %><% end %>
</li>

0 comments on commit 24ca24f

Please sign in to comment.