Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature to convert from condition to ifdef. #1

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
47 changes: 47 additions & 0 deletions lib/docbookrx/docbook_visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,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 @@ -291,6 +295,8 @@ def append_text text, unsub = false
## Lifecycle callbacks

def before_traverse node, method
append_ifdef_start_if_condition(node)

case method.to_s
when "visit_itemizedlist", "visit_orderedlist"
@list_depth += 1
Expand Down Expand Up @@ -336,6 +342,8 @@ def after_traverse node, method
end
end
end

append_ifdef_end_if_condition(node)
end

## Node visitor callbacks
Expand Down Expand Up @@ -1035,6 +1043,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 @@ -1045,6 +1054,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 @@ -1563,5 +1573,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