Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Russ Olsen
committed
Jan 13, 2012
1 parent
fe3970b
commit 8282df9
Showing
9 changed files
with
238 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
require 'rexml/document' | ||
require 'pp' | ||
|
||
class DocbookRenderer < Processor | ||
include Tokenize | ||
include REXML | ||
|
||
def process( paragraphs ) | ||
@author = 'John Smith' | ||
@title = 'Wealth of Nations' | ||
@chapters = [] | ||
@chapter = nil | ||
@section = nil | ||
|
||
paragraphs.each do |paragraph| | ||
format( paragraph ) | ||
end | ||
@book = Element.new('book') | ||
@book.add_namespace('http://docbook.org/ns/docbook') | ||
@book.add_attribute('version', '5.0') | ||
@book.add_element element_for('title', @title) | ||
@book.add_element element_for('author', @author) | ||
@chapters.each {|ch| @book << ch} | ||
doc = Document.new | ||
decl = XMLDecl.new | ||
decl.version = '1.0' | ||
doc << decl | ||
doc << @book | ||
doc | ||
end | ||
|
||
def element_for(type, text) | ||
result = Element.new(type) | ||
result.add_text(text) | ||
result | ||
end | ||
|
||
def format( p ) | ||
type = p[:type] | ||
text = p.string | ||
|
||
return nil if text.empty? and :type != :code | ||
|
||
case type | ||
when :title | ||
@title = text | ||
when :author | ||
@author = text | ||
when :chapter | ||
puts "adding chapter #{text}" | ||
@chapter = Element.new('chapter') | ||
@chapters << @chapter | ||
@section = nil | ||
title_element = Element.new('title') | ||
add_body_text(title_element, text) | ||
@chapter.add_element(title_element) | ||
when :section | ||
puts "adding section #{text}" | ||
@section = Element.new('section') | ||
@chapter.add(@section) | ||
title_element = Element.new('title') | ||
add_body_text(title_element, text) | ||
@section.add_element(title_element) | ||
when :body | ||
puts "adding body #{text[0..5]}" | ||
paragraph = Element.new('para') | ||
add_body_text(paragraph, text) | ||
add_content_element(paragraph) | ||
when :code | ||
add_content_element(code_element(type, text)) | ||
else | ||
raise "#{type}???" | ||
end | ||
end | ||
|
||
def add_content_element(el) | ||
if @section | ||
@section.add_element(el) | ||
elsif @chapter | ||
@chapter.add_element(el) | ||
else | ||
raise "No chapter to add #{el} to" | ||
end | ||
end | ||
|
||
def text_element(type, text) | ||
element = [ tag_for(type) ] | ||
add_body_text(element, text) | ||
element | ||
end | ||
|
||
def tag_for(type) | ||
case type | ||
when :body | ||
'para' | ||
when :text | ||
'p' | ||
when :author | ||
'h3' | ||
when :section | ||
'h3' | ||
when :chapter | ||
'chapter' | ||
when :sec | ||
'h3' | ||
when :title | ||
'h2' | ||
else | ||
raise "Dont know what to do with #{type}" | ||
end | ||
end | ||
|
||
def add_body_text( element, text ) | ||
tokens = tokenize_body_text( text ) | ||
tokens.each {|token| add_span( token, element ) } | ||
end | ||
|
||
def add_span( token, element ) | ||
puts "Add span: token: #{token} element: #{element}" | ||
case token[:type] | ||
when :italic | ||
element.add( span_for( token.string, "emphasis" )) | ||
when :code | ||
element.add( span_for( token.string, "code" )) | ||
when :bold | ||
element.add( span_for( token.string, "emphasis" )) | ||
when :normal | ||
element.add_text( token.string ) | ||
when :footnote | ||
element.add(footnote_for(token.string)) | ||
else | ||
raise "Dont know what to do with #{token}" | ||
end | ||
end | ||
|
||
def footnote_for( text ) | ||
fn = Element.new('footnote') | ||
fn.add_element(element_for('para', text)) | ||
fn | ||
end | ||
|
||
def code_element(type, text) | ||
element = Element.new('informalexample') | ||
prog_element = element_for('programlisting', text) | ||
prog_element.add_attribute('xml:space', 'preserve') | ||
element.add_element(prog_element) | ||
element | ||
end | ||
|
||
def span_for( text, style ) | ||
span = Element.new(style) | ||
span.text = remove_escapes(text) | ||
span | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
.title The Roots of Ruby | ||
.author Fogus and Russ | ||
.chapter The Way of the World | ||
AAAAA In more | ||
|
||
this is ~~italics~~ and | ||
|
||
this is a footnote {{foot notes}} and | ||
|
||
this is @@code@@ and | ||
|
||
this is !!bold!! is book. | ||
All DocBook elements are in an XML Namespace, so the root element has an xmlns attribute to set the current namespace. Also, the root element of a DocBook document must have a version that specifies the version of the format that the document is built on. | ||
|
||
.section A section | ||
In more detail, the root element of the document is book. All DocBook elements are in an XML Namespace, so the root element has an xmlns attribute to set the current namespace. Also, the root element of a DocBook document must have a version that specifies the version of the format that the document is built on. | ||
Some code | ||
.code | ||
|
||
# A comment surrouded by white space | ||
|
||
|
||
# Another comment | ||
|
||
|
||
|
||
.body | ||
body line | ||
.chapter A second chapter | ||
More intro text | ||
.section Section 1 | ||
section 1 text | ||
more body | ||
text | ||
.code | ||
foo = bar | ||
bar = foo | ||
.body | ||
still more body text | ||
.section Two | ||
section 2 text |