Class: AsciidoctorDitaMap::Convert
- Inherits:
-
Object
- Object
- AsciidoctorDitaMap::Convert
- Defined in:
- lib/dita-map/convert.rb
Instance Attribute Summary collapse
-
#attr ⇒ Object
Returns the value of attribute attr.
-
#opts ⇒ Object
Returns the value of attribute opts.
-
#prep ⇒ Object
Returns the value of attribute prep.
Instance Method Summary collapse
- #compose_mapref_attributes(element, file_info, type) ⇒ Object
- #compose_topicref_attributes(element, file_info, title, type) ⇒ Object
-
#initialize ⇒ Convert
constructor
A new instance of Convert.
- #run(input, base_dir, file = nil) ⇒ Object
Constructor Details
#initialize ⇒ Convert
Returns a new instance of Convert.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/dita-map/convert.rb', line 32 def initialize @attr = [] @opts = { :chunk => true, :id => true, :assembly => true, :locktitle => true, :navtitle => true, :title => true, :toc => true, :type => true, :self => false, :verbose => false, :zero_offset => false } @prep = '' end |
Instance Attribute Details
#attr ⇒ Object
Returns the value of attribute attr.
30 31 32 |
# File 'lib/dita-map/convert.rb', line 30 def attr @attr end |
#opts ⇒ Object
Returns the value of attribute opts.
30 31 32 |
# File 'lib/dita-map/convert.rb', line 30 def opts @opts end |
#prep ⇒ Object
Returns the value of attribute prep.
30 31 32 |
# File 'lib/dita-map/convert.rb', line 30 def prep @prep end |
Instance Method Details
#compose_mapref_attributes(element, file_info, type) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/dita-map/convert.rb', line 50 def compose_mapref_attributes element, file_info, type target_file = file_info[:target].sub(/\.adoc$/, '.ditamap') element.add_attribute 'href', target_file element.add_attribute 'format', 'ditamap' element.add_attribute 'type', type if @opts[:type] element.add_attribute 'chunk', file_info[:chunk] if @opts[:chunk] and file_info[:chunk] element.add_attribute 'toc', file_info[:toc] if @opts[:toc] and file_info[:toc] end |
#compose_topicref_attributes(element, file_info, title, type) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/dita-map/convert.rb', line 60 def compose_topicref_attributes element, file_info, title, type target_file = file_info[:target].sub(/\.adoc$/, '.dita') element.add_attribute 'href', target_file if file_info[:navtitle] element.add_attribute REXML::Attribute.new('navtitle', file_info[:navtitle]) if @opts[:navtitle] else element.add_attribute REXML::Attribute.new('navtitle', title) if @opts[:navtitle] and title end element.add_attribute 'locktitle', 'yes' if @opts[:locktitle] and element['navtitle'] element.add_attribute 'type', type if @opts[:type] and type and ['concept', 'reference', 'task'].include? type element.add_attribute 'chunk', file_info[:chunk] if @opts[:chunk] and file_info[:chunk] element.add_attribute 'toc', file_info[:toc] if @opts[:toc] and file_info[:toc] end |
#run(input, base_dir, file = nil) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/dita-map/convert.rb', line 77 def run input, base_dir, file = nil result = '' map = Map.new @prep + input, base_dir, @attr xml = REXML::Document.new xml.context[:attribute_quote] = :quote xml << REXML::XMLDecl.new('1.0', 'utf-8') xml << REXML::DocType.new('map', 'PUBLIC "-//OASIS//DTD DITA Map//EN" "map.dtd"') if map.id and @opts[:id] xml_root = xml.add_element('map', { 'id' => map.id }) else xml_root = xml.add_element('map') end if map.title and @opts[:title] xml_title = xml_root.add_element('title') xml_title.add REXML::Text.new(map.title, false, nil, true) end if @opts[:self] and file xml_self = xml_root.add_element('topicref') compose_topicref_attributes(xml_self, { :target => file }, map.title, map.type) stack = [{ :offset => 0, :element => xml_self }] else stack = [{ :offset => 0, :element => xml_root }] end map.includes.each do |file_info| target = file_info[:target] offset = file_info[:offset] last_offset = stack.last[:offset] full_path = base_dir + target if not File.exist? full_path and @opts[:verbose] warn "#{NAME}: warning: file not found: #{target}" end begin topic = Topic.new @prep + File.read(full_path), @attr next if ['attributes', 'snippet'].include? topic.type rescue warn "#{NAME}: warning: unable to read included file: #{target}" topic = Topic.new '' end if offset == 0 if @opts[:zero_offset] offset = 0 else warn "#{NAME}: warning: invalid leveloffset - expected 1, got 0: #{target}" offset = 1 end elsif offset > last_offset and offset - last_offset > 1 expected_offset = last_offset + 1 warn "#{NAME}: warning: invalid leveloffset - expected #{expected_offset}, got #{offset}: #{target}" end while stack.length > 1 and stack.last[:offset] >= offset stack.pop end xml_parent = stack.last[:element] topic.type.sub!(/^assembly$/, 'concept') if not @opts[:assembly] if topic.type == 'map' or topic.type == 'assembly' xml_element = xml_parent.add_element('mapref') compose_mapref_attributes xml_element, file_info, 'map' else xml_element = xml_parent.add_element('topicref') compose_topicref_attributes xml_element, file_info, topic.title, topic.type end stack.push ({ :offset => offset, :element => xml_element }) end formatter = REXML::Formatters::Pretty.new(2, true) formatter.compact = true formatter.write(xml, result) result << "\n" return result end |