docgenerator 1.1.1 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. data/examples/docgenerator_example.rb +34 -17
  2. data/examples/docgenerator_example_footnote.rb +82 -61
  3. data/examples/docgenerator_example_list.rb +24 -18
  4. data/examples/docgenerator_example_restrictions.rb +17 -27
  5. data/examples/docgenerator_example_tabular.rb +20 -20
  6. data/examples/docgenerator_example_tripfalls.rb +28 -20
  7. data/examples/results/readme +1 -0
  8. data/examples/results_expected/docgenerator_example.html +39 -0
  9. data/examples/results_expected/docgenerator_example.pdf +0 -0
  10. data/examples/results_expected/docgenerator_example.tex +56 -0
  11. data/examples/results_expected/docgenerator_example_footnote.html +40 -0
  12. data/examples/results_expected/docgenerator_example_footnote.pdf +0 -0
  13. data/examples/results_expected/docgenerator_example_footnote.tex +50 -0
  14. data/examples/results_expected/docgenerator_example_list.html +18 -0
  15. data/examples/results_expected/docgenerator_example_list.pdf +0 -0
  16. data/examples/results_expected/docgenerator_example_list.tex +46 -0
  17. data/examples/results_expected/docgenerator_example_restrictions.html +22 -0
  18. data/examples/results_expected/docgenerator_example_restrictions.pdf +0 -0
  19. data/examples/results_expected/docgenerator_example_restrictions.tex +31 -0
  20. data/examples/results_expected/docgenerator_example_tabular.html +37 -0
  21. data/examples/results_expected/docgenerator_example_tabular.pdf +0 -0
  22. data/examples/results_expected/docgenerator_example_tabular.tex +41 -0
  23. data/examples/results_expected/docgenerator_example_tripfalls.html +27 -0
  24. data/examples/results_expected/docgenerator_example_tripfalls.pdf +0 -0
  25. data/examples/results_expected/docgenerator_example_tripfalls.tex +31 -0
  26. data/lib/creole/creole2doc.rb +291 -140
  27. data/lib/creole/creole_inclusion_and_plugins.rb +142 -39
  28. data/lib/creole/creole_placeholder.rb +92 -30
  29. data/lib/creole/creole_tabular.rb +119 -20
  30. data/lib/docgenerator.rb +209 -118
  31. data/lib/{docgenerator_attribute.rb → docgenerator/attribute.rb} +2 -1
  32. data/lib/{docgenerator_characters.rb → docgenerator/characters.rb} +75 -6
  33. data/lib/{docgenerator_css.rb → docgenerator/css.rb} +0 -0
  34. data/lib/docgenerator/document.rb +588 -0
  35. data/lib/{docgenerator_element.rb → docgenerator/element.rb} +52 -80
  36. data/lib/{docgenerator_elements.rb → docgenerator/elements.rb} +43 -17
  37. data/lib/{docgenerator_environments.rb → docgenerator/environments.rb} +20 -6
  38. data/lib/{docgenerator_footnote.rb → docgenerator/footnote.rb} +22 -18
  39. data/lib/{docgenerator_lists.rb → docgenerator/lists.rb} +17 -1
  40. data/lib/{docgenerator_sections.rb → docgenerator/sections.rb} +9 -1
  41. data/lib/docgenerator/standard.rb +81 -0
  42. data/lib/{docgenerator_tabular.rb → docgenerator/tabular.rb} +9 -5
  43. data/lib/packages/docgenerator_listings.rb +9 -5
  44. data/lib/packages/docgenerator_pdfpages.rb +17 -11
  45. data/lib/packages/docgenerator_url.rb +0 -2
  46. data/lib/templates/docgenerator_template.rb +66 -29
  47. data/lib/templates/docgenerator_template.yaml +18 -0
  48. data/lib/wiki2doc/wiki2docgenerator.rb +6 -2
  49. data/readme.rd +132 -0
  50. data/unittest/expected/test_document_usepackage_undefined.log +2 -0
  51. data/unittest/expected/test_enumerate.context +4 -0
  52. data/unittest/expected/test_footnote.html +4 -4
  53. data/unittest/expected/test_footnote.latex +4 -4
  54. data/unittest/expected/test_footnote_group.html +6 -6
  55. data/unittest/expected/test_footnote_group.latex +6 -6
  56. data/unittest/expected/test_href.html +2 -2
  57. data/unittest/expected/test_href.latex +2 -2
  58. data/unittest/expected/test_href.text +2 -2
  59. data/unittest/expected/test_itemize.context +4 -0
  60. data/unittest/expected/test_runtex.stdout +3 -0
  61. data/unittest/expected/test_runtex_2.stdout +3 -0
  62. data/unittest/expected/test_section.context +20 -0
  63. data/unittest/expected/test_tabular.html +2 -2
  64. data/unittest/expected/test_verbatim.html +3 -0
  65. data/unittest/expected/test_verbatim.latex +4 -0
  66. data/unittest/expected/test_verbatim_array.html +1 -0
  67. data/unittest/expected/test_verbatim_array.latex +2 -0
  68. data/unittest/expected_creole/test_creole_characters_all.html +12 -5
  69. data/unittest/expected_creole/test_creole_characters_all.latex +20 -6
  70. data/unittest/expected_creole/test_creole_comment.context +11 -0
  71. data/unittest/expected_creole/test_creole_comment.creole +12 -0
  72. data/unittest/expected_creole/test_creole_comment.html +9 -0
  73. data/unittest/expected_creole/test_creole_comment.latex +11 -0
  74. data/unittest/expected_creole/test_creole_creole1.0test.html +3 -3
  75. data/unittest/expected_creole/test_creole_creole1.0test.latex +3 -3
  76. data/unittest/expected_creole/test_creole_creole1.0test.text +4 -4
  77. data/unittest/expected_creole/test_creole_ignore.html +3 -0
  78. data/unittest/expected_creole/test_creole_ignore.latex +4 -0
  79. data/unittest/expected_creole/test_creole_ignore.text +2 -0
  80. data/unittest/expected_creole/test_creole_input.normsource +12 -12
  81. data/unittest/expected_creole/test_creole_input_file_simple.html +15 -0
  82. data/unittest/expected_creole/test_creole_input_file_simple_readlines.html +15 -0
  83. data/unittest/expected_creole/test_creole_links_external_implicit.html +2 -2
  84. data/unittest/expected_creole/test_creole_links_external_implicit.latex +2 -2
  85. data/unittest/expected_creole/test_creole_links_external_implicit.text +2 -2
  86. data/unittest/expected_creole/test_creole_links_implicit_end.creole +4 -0
  87. data/unittest/expected_creole/test_creole_links_implicit_end.html +3 -0
  88. data/unittest/expected_creole/test_creole_links_implicit_end.latex +4 -0
  89. data/unittest/expected_creole/test_creole_list_ul.normsource +42 -42
  90. data/unittest/expected_creole/test_creole_mix_titles_list.html +10 -0
  91. data/unittest/expected_creole/test_creole_mix_titles_list.latex +18 -0
  92. data/unittest/expected_creole/test_creole_mix_titles_list.text +12 -0
  93. data/unittest/expected_creole/test_creole_paragraphs.normsource +15 -15
  94. data/unittest/expected_creole/test_creole_pictures_css.html +4 -1
  95. data/unittest/expected_creole/test_creole_pictures_css.latex +5 -1
  96. data/unittest/expected_creole/test_creole_pictures_longdesc.html +3 -0
  97. data/unittest/expected_creole/test_creole_pictures_width.html +3 -0
  98. data/unittest/expected_creole/test_creole_pictures_width.latex +4 -0
  99. data/unittest/expected_creole/test_creole_short_html.html +3 -0
  100. data/unittest/expected_creole/test_creole_short_html.latex +4 -0
  101. data/unittest/expected_creole/test_creole_short_html.text +2 -0
  102. data/unittest/expected_creole/test_creole_shy.creole +6 -0
  103. data/unittest/expected_creole/test_creole_shy.html +6 -0
  104. data/unittest/expected_creole/test_creole_shy.latex +8 -0
  105. data/unittest/expected_creole/test_creole_shy.text +4 -0
  106. data/unittest/expected_creole/test_creole_tabular_with_cell_option.html +20 -0
  107. data/unittest/expected_creole/test_creole_toc.html +3 -1
  108. data/unittest/expected_creole/test_creole_toc.latex +1 -1
  109. data/unittest/expected_creole/test_creole_toc.text +1 -1
  110. data/unittest/expected_creole/test_creole_toc_level.html +7 -0
  111. data/unittest/expected_creole/test_creole_toc_level.latex +10 -0
  112. data/unittest/expected_creole/test_creole_toc_level.text +3 -0
  113. data/unittest/expected_creole/test_creole_toc_level_text.html +8 -0
  114. data/unittest/expected_creole/test_creole_toc_level_text.latex +9 -0
  115. data/unittest/expected_creole/test_creole_verbatim.html +1 -1
  116. data/unittest/expected_creole/test_creole_verbatim.latex +1 -1
  117. data/unittest/expected_creole/test_creole_verbatim.text +1 -1
  118. data/unittest/expected_creole/test_creole_verbatim_inline.html +1 -1
  119. data/unittest/expected_creole/test_creole_verbatim_inline.latex +1 -1
  120. data/unittest/expected_wiki/test_wiki_description.html +5 -5
  121. data/unittest/expected_wiki/test_wiki_description.latex +5 -5
  122. data/unittest/expected_wiki/test_wiki_description.text +5 -5
  123. data/unittest/expected_wiki/test_wiki_description.wiki +5 -5
  124. data/unittest/test_rakefile_docgenerator.rb +43 -0
  125. data/unittest/unittest_creole2doc.rb +415 -27
  126. data/unittest/unittest_docgenerator.rb +256 -23
  127. data/unittest/unittest_docgenerator_runtex.rb +83 -0
  128. data/unittest/unittest_versions.rb +44 -0
  129. data/unittest/unittest_wiki2doc.rb +28 -27
  130. metadata +473 -106
  131. data/lib/docgenerator_document.rb +0 -525
  132. data/lib/packages/docgenerator_beamer.rb +0 -253
  133. data/lib/yaml2presentation/yaml2presentation.rb +0 -1091
  134. data/unittest/expected/beamer_01_article.tex +0 -13
  135. data/unittest/expected/beamer_01_attachment.tex +0 -15
  136. data/unittest/expected/beamer_01_doc.html +0 -13
  137. data/unittest/expected/beamer_01_doc.tex +0 -18
  138. data/unittest/expected/beamer_01_notes_only.tex +0 -13
  139. data/unittest/expected/beamer_01_notes_onlyslideswithnotes.tex +0 -13
  140. data/unittest/expected/beamer_01_notes_show.tex +0 -13
  141. data/unittest/expected/beamer_01_presentation.tex +0 -13
  142. data/unittest/expected/beamer_02_doc.html +0 -29
  143. data/unittest/expected/beamer_02_doc.tex +0 -34
  144. data/unittest/expected/beamer_block.html +0 -1
  145. data/unittest/expected/beamer_block.latex +0 -1
  146. data/unittest/expected/beamer_doc_fragile_doc.tex +0 -33
  147. data/unittest/expected/beamer_doc_html_overview_with_detailpic_and_text_doc.html +0 -34
  148. data/unittest/expected/beamer_doc_html_overview_with_detailpic_doc.html +0 -34
  149. data/unittest/expected/beamer_doc_key_doc.html +0 -20
  150. data/unittest/expected/beamer_doc_key_doc.tex +0 -29
  151. data/unittest/expected/beamer_doc_note_doc.html +0 -30
  152. data/unittest/expected/beamer_doc_note_doc.tex +0 -37
  153. data/unittest/expected/beamer_doc_pic_bottom_doc.html +0 -24
  154. data/unittest/expected/beamer_doc_pic_doc.html +0 -24
  155. data/unittest/expected/beamer_doc_pic_doc.tex +0 -28
  156. data/unittest/expected/beamer_doc_pic_left_doc.html +0 -24
  157. data/unittest/expected/beamer_doc_pic_left_doc.tex +0 -28
  158. data/unittest/expected/beamer_doc_pic_ratio_doc.tex +0 -29
  159. data/unittest/expected/beamer_doc_pic_right_doc.html +0 -24
  160. data/unittest/expected/beamer_doc_pic_right_doc.tex +0 -28
  161. data/unittest/expected/beamer_doc_pic_top_doc.html +0 -24
  162. data/unittest/expected/beamer_frame.html +0 -5
  163. data/unittest/expected/beamer_frame.tex +0 -11
  164. data/unittest/expected/beamer_frame_fragile.tex +0 -7
  165. data/unittest/expected/beamer_frame_id.html +0 -1
  166. data/unittest/expected/beamer_frame_id.tex +0 -4
  167. data/unittest/expected/beamer_framepic.html +0 -5
  168. data/unittest/expected/beamer_framepic.latex +0 -1
  169. data/unittest/expected/beamer_framepic_link.html +0 -5
  170. data/unittest/expected/beamer_framepic_link.latex +0 -1
  171. data/unittest/expected/beamer_frametitle.html +0 -1
  172. data/unittest/expected/beamer_frametitle.latex +0 -1
  173. data/unittest/expected/beamer_note.html +0 -1
  174. data/unittest/expected/beamer_note.latex +0 -1
  175. data/unittest/expected_privat/test_creole_test_document.html +0 -6
  176. data/unittest/expected_wiki/test_wiki_amazon.html +0 -10
  177. data/unittest/expected_wiki/test_wiki_link.log +0 -3
  178. data/unittest/test_docgenerator.rb +0 -107
  179. data/unittest/unittest_yaml2pres.rb +0 -336
@@ -1,525 +0,0 @@
1
- #
2
- #Container for a document.
3
- #
4
- #Each document contains a header and a body. Both are special Element classes.
5
- #
6
- class Document
7
- #Valid meta tags, used for HTML
8
- #
9
- #This meta-tags are used by sitegenerator
10
- VALID_META_TAGS = %w{
11
- keywords
12
- description
13
- id
14
- altlang
15
- toc
16
- pdf
17
- odp
18
- amazon
19
- forum
20
- feed
21
- }
22
- class << VALID_META_TAGS
23
- # Return true if self is part of the given parameter.
24
- def ===(o)
25
- return self.include?(o)
26
- end
27
- end
28
-
29
- #Create a new document.
30
- #There are different document templates supported.
31
- #The templates refer to the corresponding TeX-classes.
32
- #- article
33
- #- report
34
- #- book
35
- def initialize( settings = {} )
36
- #Set template defaults
37
- @template = {
38
- :html => DocumentTemplate[:html],
39
- :latex => DocumentTemplate[:article],
40
- :text => DocumentTemplate[:text],
41
- :wiki => DocumentTemplate[:wiki],
42
- }
43
- @language = 'ngerman' #default
44
- @date = nil
45
-
46
- ##>>>>temporary check, old interface
47
- #~ puts settings.inspect
48
- if ! settings.is_a?(Hash)
49
- #Make an exception to correct data
50
- raise "Old interface for Document#new, use :template => "
51
- settings = { :template => settings }
52
- end
53
- ##<<<<temporary check, old interface
54
-
55
- @log = settings[:log]
56
- if ! @log
57
- @log = Log4r::Logger.new( 'Doc', Log4r::WARN )
58
- @log.outputters = Log4r::StdoutOutputter.new('log_xxx')
59
- end
60
- @meta = {} #some meta-Tags for HTML
61
- @body = element( :body )
62
- @head = element( :head )
63
- #~ @body.part_of << self
64
- #~ @head.part_of << self
65
- @options = []
66
- #Flag to avoid double definition of docinfo()
67
- @docinfo_called = false
68
- @creator = nil
69
-
70
- settings.each{|key, value|
71
- case key
72
- when :title, :shorttitle, :author, :date, :keywords, :description, :creator, :language
73
- self.send("#{key}=".to_sym, value)
74
- #~ when :odp, :pdf, :amazon #VALID_META_TAGS
75
- when *VALID_META_TAGS.map{|mkey|mkey.to_sym}
76
- self.meta( key, value )
77
- when :template
78
- [value].flatten.each{|template|
79
- case template
80
- when DocumentTemplate
81
- @template[template.target] = template
82
- when Symbol
83
- templ = DocumentTemplate[template]
84
- if ! templ.is_a?( DocumentTemplate )
85
- @log.fatal("Unknown template #{template}, valid:\n\t#{DocumentTemplate.keys.join("\n\t")}") if @log.fatal?
86
- else
87
- @template[templ.target] = templ
88
- end
89
- end
90
- } #templates
91
- when :css
92
- if ! File.exist?(value)
93
- @log.warn("CSS-Reference not found (#{value})") if @log.warn?
94
- end
95
- #~ @head << element(:htmlonly,{}, <<css
96
- #~ <meta http-equiv="Content-Style-Type" content="text/css">
97
- #~ css
98
- #~ <link rel="stylesheet" type="text/css" href="#{value}">
99
- #~ ).cr
100
- @head << element( :link, {
101
- :href => value,
102
- :rel => 'stylesheet',
103
- :type => "text/css" } ).cr. restrict_to(:html)
104
- when :maketitle
105
- #:maketitle is defined, but we have also to check, if it is true
106
- @body << element(:maketitle ).cr if settings[key]
107
- when :log
108
- else
109
- @log.warn("Document.new: Unknown setting #{key.inspect} (=#{value.inspect})") if @log.warn?
110
- end
111
- } #settings
112
-
113
- end
114
- #body and head are lists, containing elements.
115
- attr_reader :body, :head
116
- attr_reader :template
117
- #Logger for the document
118
- attr_reader :log
119
- #Document title
120
- attr_accessor :title, :shorttitle
121
- #Set document description
122
- attr_writer :author, :date, :keywords, :description, :creator
123
- attr_writer :language
124
- #Flag, if tex should be started immediate after document creation.
125
- attr_writer :runtex
126
- #Add more keywords
127
- def keyword_add( keyword )
128
- @keywords << ", #{keyword}"
129
- end
130
-
131
- #Constant to detect the generated header
132
- PREFIX_ENDFLAG = 'Generation-Info-End'
133
- #
134
- #Define, if there should be a message in case of:
135
- #-:change Document changed
136
- #-:nochange Document existed, but is unchanged
137
- #Give an array with the wanted values.
138
- def Document.givemessage=( value = [:change, :nochange] )
139
- @@givemessage = value
140
- end
141
- @@givemessage = [:change, :nochange]
142
- def Document.givemessage(); @@givemessage; end
143
- #Add a meta-tag-information for the HTML-Output.
144
- def meta( key, content )
145
- key = key.to_s
146
-
147
- if ! VALID_META_TAGS.include?(key)
148
- @log.warn("Unknown meta-tag #{key} (#{content})") if @log.warn?
149
- @log.info("\tAllowed meta-tags: #{VALID_META_TAGS.join(', ')}") if @log.info?
150
- end
151
- if @meta[key]
152
- @log.warn("Double definition meta-tag #{key} (old: #{@meta[key]}, new: #{content})") if @log.warn? and @meta[key] != content
153
- end
154
-
155
- @meta[key] = content
156
- end
157
- #Prepare the docinfo.
158
- #May be called only once.
159
- #
160
- #If the title should change (e.g. when you save the document twice in different versions)
161
- #you can do it via the parameter.
162
- def docinfo()
163
- #If this method is called twice (e.g. with save to tex and html),
164
- #then the title is doubled in the second output.
165
- #(Elements are added to @head)
166
- return nil if @docinfo_called
167
- @docinfo_called = true
168
-
169
- #Build docinfo.
170
- result = []
171
- result << element( :title, {:short => @shorttitle }, @title).CR if @title
172
- result << element( :author, {}, @author).cr if @author
173
- result << element( :date, {}, @date).cr if @date
174
- result << element( :keywords, {}, @keywords).cr if @keywords
175
- result << element( :metadescription, {}, @description).cr if @description
176
- result << element( :creator, {}, @creator).cr if @creator
177
- @meta.each{ |key, content|
178
- result << element( :meta, { :name => key, :content => content } ).cr
179
- }
180
- return result
181
- end
182
- def add_option( option)
183
- @options << option
184
- end
185
- #Prepare a table of content.
186
- #
187
- #There is no sense to use it with LaTeX, better you use \tableofcontents
188
- #
189
- #Options:
190
- #- id: Id of div, needed for CSS-formatting
191
- #- tocfilename: Added as prefix to link.
192
- # can be used to make toc for in another file (cross-referencing)
193
- #- level: Depth of toc
194
- #The document should be filled before you add the toc.
195
- #You an use Element#insertbefore:
196
- # doc.body.insertbefore(start, doc.toc)
197
- def toc( options = {} )
198
- opt = {
199
- :id => 'toc',
200
- :tocfilename => nil, #Can be used to build cross references
201
- :level => 3,
202
- :log => @log
203
- }.update(options)
204
-
205
- opt[:level] = 3 unless opt[:level]
206
- opt[:log].info("Prepare Table of contents, options: #{options.keys.inspect}") if opt[:log].info?
207
- opt[:log].debug("Options: #{[:id, :level, :tocfilename].map{|k| "#{k}: #{opt[k].inspect}"}.join(',')}") if opt[:log].debug?
208
-
209
- toc = element(:div, { :id => opt[:id] }).cR
210
- opt[:pre].is_a?(String) ? toc << element(:p, {}, opt[:pre] ).cr : toc << opt[:pre]
211
-
212
- toc_uls = [ toc ]
213
-
214
- #fixme
215
- #It would be nicer to get something like 1-1-1 instead of a counter.
216
- toccnt = 0
217
- toc_ids = [ :chapter, :section,:subsection, :subsubsection, :paragraph, :subparagraph,:minisec]
218
- @body.content.flatten.each{|el|
219
- if ! el.respond_to?(:ids)
220
- #e.g. Wikitext. Has a to_doc, but doesn't react on flatten.
221
- opt[:log].error("Toc: Element ohne :ids #{el.class}") if opt[:log].error?
222
- next
223
- end
224
- depth = toc_ids.index((el.ids & toc_ids)[0])
225
- next if ! depth #Element is no header
226
-
227
- opt[:log].debug("Toc: Found entry on level #{depth}: <#{el.content}>") if opt[:log].debug?
228
- #Create an id if not already defined
229
- if el[:id].content.empty?
230
- el[:id] << "toc#{toccnt += 1}"
231
- end
232
- if depth > opt[:level]
233
- opt[:log].debug("Toc: Reject by level #{el.content}") if opt[:log].debug?
234
- next
235
- end
236
-
237
- #Check missing levels
238
- (depth - toc_uls.size).times{|i|
239
- opt[:log].warn("Toc: Jump over section level #{depth-1+i} (#{opt[:tocfilename]})") if opt[:log].warn?
240
- toc_uls << element(:ul, {}, element(:li ).cr ).cR
241
- if toc_uls.size == 2 #First entry
242
- toc_uls.first << toc_uls.last
243
- else
244
- toc_uls[-2].content.last << toc_uls.last
245
- end
246
- }
247
-
248
- #Build new toc-list and deletet bigger ones
249
- if ! toc_uls[depth]
250
- opt[:log].debug("Toc: Start new level #{depth}") if opt[:log].debug?
251
- toc_uls << element(:ul).cR
252
- if depth == 1
253
- toc << toc_uls[depth]
254
- else
255
- toc_uls[depth -1 ].content.last << toc_uls[depth]
256
- end
257
- else #delete levels under actual level
258
- if toc_uls.slice!(depth+1..toc_uls.size+1)
259
- opt[:log].debug("Toc: Stop level > #{depth}") if opt[:log].debug?
260
- end
261
- end
262
- toc_uls[depth] << element(:li, {}, element(:a, {:href => "#{opt[:tocfilename]}##{el[:id]}" }, el.content ) ).cr
263
- }
264
- opt[:post].is_a?(String) ? toc << element(:p, {}, opt[:post] ).cr : toc << opt[:post]
265
-
266
- return toc
267
- end #toc
268
- #Save the file.
269
- #The type of the document is determined by the file extensison.
270
- #Supported document types are:
271
- #- tex
272
- #- html
273
- #- (wiki)
274
- #- (text)
275
- #Depending on a template, different results are created.
276
- #
277
- #There is a comparison between an already existing file and the new one.
278
- #To write a new version, the document must change and overwrite must be true.
279
- #
280
- #Valid options:
281
- #- :replacements
282
- # It is possible to give pairs of RegExp (pattern) and replacements to the result.
283
- #- overwrite [true]
284
- # allows to overwrite
285
- #- :additional_options
286
- # List of options.
287
- # If you want to give parameters to specific 'Element'-obejcts you can define the keys here.
288
- # If you don't make it, you get warnings about undefined options.
289
- #
290
- def save( filename, options = {} )
291
- #check old interface before 1.1.0
292
- case options
293
- when true, false
294
- @log.warn( "Old interface for Document.save, use :overwrite => true/false" ) if @log.error?
295
- options = { :overwrite => options }
296
- end
297
-
298
- options = {
299
- :overwrite => true,
300
- :replacements => {} ,
301
- :additional_options => [],
302
- }.update(options)
303
-
304
- #Check valid options
305
- options.each{|key,value|
306
- case key
307
- when :overwrite, :replacements
308
- when :additional_options
309
- else
310
- if options[:additional_options].include?(key)
311
- @log.warn( "unknown option #{key.inspect} for Document.save" ) if @log.warn?
312
- else
313
- @log.debug( "Save-Specific option #{key.inspect} in Document.save" ) if @log.debug?
314
- end
315
- end
316
- }
317
-
318
- if ! @template
319
- @log.error( "No template available to create #{filename}" ) if @log.error?
320
- return false
321
- end
322
-
323
- #Get old content for comparison
324
- old = nil
325
- prefix = [ nil,
326
- "Build by\t#{__FILE__}",
327
- "Dir:\t\t#{Dir.pwd}",
328
- "Creator:\t#{$0}",
329
- "Target:\t\t#{filename}",
330
- "#{Time.now.strftime('%Y/%m/%d %H:%M:%S')}",
331
- nil,
332
- "#{PREFIX_ENDFLAG}"
333
- ].join("\n\t")
334
- if File.exist?( filename )
335
- old = File.read( filename )
336
- end
337
- #Determine the target document type, depending on extension.
338
- extension = File.basename( filename ).split( /\./).last
339
- case extension
340
- when /tex/i
341
- target = :latex
342
- when /htm[l]?/i
343
- target = :html
344
- when /txt/
345
- target = :text
346
- when /wiki/
347
- target = :wiki
348
- else
349
- @log.fatal( "Unknown Extension #{extension} for #{File.basename( filename )}" ) if @log.fatal?
350
- raise "Unknown Extension #{extension}"
351
- end
352
-
353
- new = to_doc( target, {
354
- :template=> @template[target],
355
- :filename => filename,
356
- :replacements => options[:replacements],
357
- :log => @log
358
- })
359
-
360
- case target
361
- when :latex
362
- prefix.gsub!( /^/, '%' )
363
- old.sub!(/\A.*#{PREFIX_ENDFLAG}/m, '<<prefix>>' ) if old
364
- when :html
365
- #Delete prefix (with generation time) for later compare.
366
- old.sub!(/\A.*#{PREFIX_ENDFLAG}/m, "<!--\n<<prefix>>" ) if old
367
- when :text
368
- when :wiki
369
- #~ new.squeeze!("\n")
370
- else
371
- @log.fatal( "Unknown target #{target} for #{File.basename( filename )}" ) if @log.fatal?
372
- raise "Unknown target #{target}"
373
- end
374
- #Make it a bit more compact.
375
- #TeX requires at least 2 \n for paragraph changes
376
- new.gsub!(/\n+\n\n/, "\n\n")
377
-
378
- if ! new.kind_of?( String )
379
- @log.error( "New is wrong type: #{new.inspect}" ) if @log.error?
380
- end
381
-
382
- if new != old
383
- new.sub!( '<<prefix>>', prefix)
384
- if ( File.exist?( filename ) and ! options[:overwrite] )
385
- puts "Datei #{filename} exist already \nContinue [yn]?"
386
- answer = $stdin.gets() if $stdin.tty? #nur bei call aus DOS-Box
387
- if ! ( answer =~ /[YyjJ].*/ )
388
- puts "Bye"
389
- @log.info( "Overwrite #{filename} after confirmation" ) if @log.info?
390
- return false
391
- end
392
- end
393
- f = File.new( filename, 'w' )
394
- f << new
395
- f.close
396
- @log.info( "Save changed\t#{filename}") if @log.info?
397
- puts "Save changed\t#{filename}" if @@givemessage.include?(:change)
398
- #Save copy of old version (attention, *.bak makes no control on tex or html)
399
- #~ f = File.new( filename.sub( extension, 'bak'), 'w' )
400
- #~ f << old
401
- #~ f.close
402
- Document.runtex( filename, @runtex ) if @runtex and target == :latex
403
- return true
404
- elsif old
405
- @log.info("Unchanged\t#{filename}") if @log.info?
406
- puts "Unchanged\t#{filename}" if @@givemessage.include?(:nochange)
407
- return false
408
- end
409
- end #Document#save
410
- #Build the content for the target format.
411
- #Supported target formats are:
412
- #- :latex
413
- #- :html
414
- #- :text
415
- #If the standard templates are used, there is a <<prefix>> left
416
- #(used from Document#save to include some additional information).
417
- #
418
- #If the method is called directly to prepare document snipplets, you can use:
419
- # puts Document#to_doc( :latex, '<<body>>')
420
- #
421
- #It is possible to give pairs of RegExp (pattern) and a replacement to the result.
422
- def to_doc( target, options )
423
- #~ o = Docgenerator_logger.set_option_defaults(options)
424
- options[:log].info( "Build document for #{target.inspect}" ) if options[:log].info?
425
- template = options[:template]
426
- if ! template
427
- options[:log].error( "No template available to create #{target.inspect} #{self.class}" ) if options[:log].error?
428
- return ''
429
- end
430
- replacements = options[:replacements] || {}
431
- options[:log].warn( "No filename given " ) if options[:log].warn? and ! options[:filename]
432
-
433
- new = template.template
434
- case target
435
- when :latex
436
- add_option( @language )
437
- when :html
438
- when :text
439
- when :wiki
440
- else
441
- options[:log].fatal( "Unknown target #{target} for #{options[:filename]}" ) if options[:log].fatal?
442
- return ''
443
- end
444
- if ! new.kind_of?( String )
445
- options[:log].error( "New is wrong type: #{new.inspect}") if options[:log].error?
446
- end
447
-
448
- options[:document] = self
449
- @head << self.docinfo()
450
- options[:log].debug( "Build header data") if options[:log].debug?
451
- new.sub!( '<<head>>', @head.to_doc(target, options ))
452
- options[:log].debug( "Build body") if options[:log].debug?
453
- new.sub!( '<<body>>', @body.to_doc(target, options))
454
- new.sub!( '<<classoptions>>', @options.uniq.join(','))
455
-
456
-
457
- replacements.each{|pattern, replace |
458
- options[:log].info("Replace text #{pattern.source} with #{replace}" ) if options[:log].info?
459
- new.gsub!( pattern, replace ) if replace
460
- }
461
- return new
462
- end #Document#to_doc
463
- #Return the actual target type.
464
- #Needed by Element#to_s to decide which method is used to prepare the output.
465
- #
466
- #The usage of this technic makes similar processing unposibble.
467
- #Set in Document#save.
468
- attr_reader :target
469
-
470
- #Call TeX and translate the file.
471
- #Experimental, unpublished feature up to now.
472
- #Fixme: Chain
473
- def Document.runtex( filename, format = :pdflualatex )
474
- begin
475
- require 'runtex'
476
- #~ require 'c:/usr/script/runtex/lib/runtex'
477
- rescue LoadError
478
- puts "Sorry, I didn't find the experimental tex translation tool to translate #{filename}"
479
- return false
480
- end
481
- #~ puts "Unknown texfile #{filename}" if ! filename
482
- tex = RunTeX::Job_chain.new( filename,
483
- :format => format )
484
- #~ case format
485
- #~ when true: tex.pdflualatex() #default
486
- #~ when false, nil
487
- #~ when :pdflatex; tex.pdflatex()
488
- #~ when :pdflualatex; tex.pdflualatex()
489
- #~ when :xelatex; tex.xelatex()
490
- #~ when :latex; tex.latex()
491
- #~ else
492
- #~ puts "Undefined chain #{format.inspect} for runtex, use pdflualatex"
493
- #~ tex.pdflualatex() #default
494
- #~ end
495
- #~ tex.file = filename
496
- begin
497
- puts "Start TeX-job #{tex.basename}"
498
- tex.log.level = Log4r::WARN
499
- tex.start(format)
500
- rescue SystemExit
501
- puts "rescued a SystemExit exception"
502
- return false
503
- end
504
- end
505
- #Make some basic replacements for TeX.
506
- #There is no sense to use it with HTML.
507
- #
508
- #Better solution: Puts String into \path, \verb or similar.
509
- def Document.texify( input )
510
- out = input.strip
511
- #~ out.gsub!( /&/, '\\\&') #geht schief. erzeugt <<body>>...
512
- #~ out.gsub!( /\\/, '\\\\')
513
- out.gsub!( /%/, '\%')
514
- out.gsub!( /\$/, '\$')
515
- out.gsub!( /&/, '\\\\&')
516
- out.gsub!( /_/, '\_')
517
- out.gsub!( /#/, '\#')
518
- out.gsub!( /�/, '\euro ')
519
- return out
520
- end
521
- def inspect()
522
- return "#<Document '#{@title}'>"
523
- #~ return "#<Document '#{@title} #{@body.inspect}>'"
524
- end
525
- end #Document