Skip to content

Commit

Permalink
Add feature to convert from condition to ifdef for #43
Browse files Browse the repository at this point in the history
  • Loading branch information
junaruga authored and Marco Rietveld committed Jun 28, 2016
1 parent 8ca7686 commit 01412b9
Show file tree
Hide file tree
Showing 4 changed files with 498 additions and 5 deletions.
22 changes: 22 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,28 @@ The script will automatically create the output file [path]_sample.adoc_, replac
The converter is not perfect yet, but we'll get there with your help.
You'll can find a list of tasks that need attention listed in the WORKLOG.adoc file.

=== Conditional attribute

The DocBook conditional attribute "condition" is replaced to "ifdef" macro.

Take note the marco is on a line by itself.

For example, in the case of there is the DocBook inline element with condition.

a<phrase condition="foo">b</phrase>c

It is converted to below AsciiDoc format.

a
ifdef::foo[]
b
endif::foo[]
c

When it is converted to html format, the output is displayed with both a space before "b" and a space after "b".

a b c

== About this Project

=== Authors
Expand Down
1 change: 1 addition & 0 deletions lib/docbookrx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def self.convert str, opts = {}
raise 'Not a parseable document' unless (root = xmldoc.root)
visitor = DocbookVisitor.new opts
root.accept visitor
visitor.after
visitor.lines * "\n"
end

Expand Down
67 changes: 62 additions & 5 deletions lib/docbookrx/docbook_visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class DocbookVisitor

LIST_NAMES = ['itemizedlist', 'orderedlist', 'variablelist', 'procedure', 'substeps', 'stepalternatives' ]

IGNORED_NAMES = ['title', 'subtitle', 'toc']

attr_reader :lines

def initialize opts = {}
Expand Down Expand Up @@ -145,6 +147,10 @@ def visit node
after_traverse node, visit_method_name if (respond_to? :after_traverse)
end

def after
replace_ifdef_lines
end

def traverse_children node, opts = {}
(opts[:using_elements] ? node.elements : node.children).each do |child|
child.accept self
Expand Down Expand Up @@ -293,6 +299,10 @@ def append_text text, unsub = false
## Lifecycle callbacks

def before_traverse node, method
unless IGNORED_NAMES.include? node.name
append_ifdef_start_if_condition(node)
end

case method.to_s
when "visit_itemizedlist", "visit_orderedlist",
"visit_procedure", "visit_substeps", "visit_stepalternatives"
Expand Down Expand Up @@ -339,6 +349,10 @@ def after_traverse node, method
end
end
end

unless IGNORED_NAMES.include? node.name
append_ifdef_end_if_condition(node)
end
end

## Node visitor callbacks
Expand All @@ -358,10 +372,10 @@ def ignore node
false
end
# Skip title and subtitle as they're always handled by the parent visitor
alias :visit_title :ignore
alias :visit_subtitle :ignore

alias :visit_toc :ignore
IGNORED_NAMES.each do |name|
method_name = "visit_#{name}".to_sym
alias_method method_name, :ignore
end

### Document node (article | book | chapter) & header node (articleinfo | bookinfo | info) visitors

Expand Down Expand Up @@ -500,7 +514,9 @@ def process_section node, special = nil
append_blank_line
append_line %([#{special}])
end
title = if (title_node = (node.at_css '> title') || (node.at_css '> info > title'))

title_node = (node.at_css '> title') || (node.at_css '> info > title')
title = if title_node
if (subtitle_node = (node.at_css '> subtitle') || (node.at_css '> info > subtitle'))
title_node.inner_html += %(: #{subtitle_node.inner_html})
end
Expand All @@ -517,8 +533,10 @@ def process_section node, special = nil
if (id = (resolve_id node, normalize: @normalize_ids)) && id != (generate_id title)
append_line %([[#{id}]])
end
append_ifdef_start_if_condition(title_node) if title_node
append_line %(#{'=' * @level} #{unwrap_text title})
lines.concat(text) unless text.nil? || text.empty?
append_ifdef_end_if_condition(title_node) if title_node
yield if block_given?
if (abstract_node = (node.at_css '> info > abstract'))
append_line
Expand Down Expand Up @@ -1087,6 +1105,7 @@ def process_table node
append_blank_line
end
(node.css '> tgroup > tbody > row').each do |row|
append_ifdef_start_if_condition(row)
append_blank_line
row.elements.each do |cell|
case cell.name
Expand All @@ -1097,6 +1116,7 @@ def process_table node
proceed cell
end
end
append_ifdef_end_if_condition(row)
end
if foot
(foot.css '> row > entry').each do |cell|
Expand Down Expand Up @@ -1615,5 +1635,42 @@ def lazy_quote text, seek = ','
def unwrap_text text
text.gsub WrappedIndentRx, ''
end

def element_with_condition? node
node.type == ELEMENT_NODE && node.attr('condition')
end

def append_ifdef_if_condition node
return unless element_with_condition?(node)
condition = node.attr('condition')
yield condition
end

def append_ifdef_start_if_condition node
append_ifdef_if_condition node do |condition|
append_line "ifdef::#{condition}[]"
end
end

def append_ifdef_end_if_condition node
append_ifdef_if_condition node do |condition|
append_line "endif::#{condition}[]"
end
end

def replace_ifdef_lines
out_lines = []
@lines.each do |line|
if (data = line.match(/^((ifdef|endif)::.+?\[\])(.+)$/))
# data[1]: "(ifdef|endif)::someting[]"
out_lines << data[1]
# data[3]: a string after "[]"
out_lines << data[3]
else
out_lines << line
end
end
@lines = out_lines
end
end
end
Loading

0 comments on commit 01412b9

Please sign in to comment.