Bayeux 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (238) hide show
  1. data/.hgrc +2 -0
  2. data/.rvmrc +27 -0
  3. data/Gemfile +42 -0
  4. data/Gemfile.lock +42 -0
  5. data/HISTORY +63 -0
  6. data/README.rdoc +19 -0
  7. data/Rakefile +89 -0
  8. data/VERSION +1 -0
  9. data/bib/Modules.bib +21 -0
  10. data/code/render/latex/active4d.render +132 -0
  11. data/code/render/latex/all_hallows_eve.render +96 -0
  12. data/code/render/latex/amy.render +171 -0
  13. data/code/render/latex/blackboard.render +111 -0
  14. data/code/render/latex/brilliance_black.render +552 -0
  15. data/code/render/latex/brilliance_dull.render +561 -0
  16. data/code/render/latex/cobalt.render +162 -0
  17. data/code/render/latex/dawn.render +126 -0
  18. data/code/render/latex/eiffel.render +132 -0
  19. data/code/render/latex/espresso_libre.render +123 -0
  20. data/code/render/latex/idle.render +93 -0
  21. data/code/render/latex/iplastic.render +99 -0
  22. data/code/render/latex/lazy.render +96 -0
  23. data/code/render/latex/mac_classic.render +135 -0
  24. data/code/render/latex/magicwb_amiga.render +117 -0
  25. data/code/render/latex/pastels_on_dark.render +204 -0
  26. data/code/render/latex/slush_poppies.render +123 -0
  27. data/code/render/latex/spacecadet.render +81 -0
  28. data/code/render/latex/sunburst.render +186 -0
  29. data/code/render/latex/twilight.render +153 -0
  30. data/code/render/latex/zenburnesque.render +126 -0
  31. data/code/render/old/txt2tags.render +131 -0
  32. data/code/render/xhtml/active4d.render +140 -0
  33. data/code/render/xhtml/all_hallows_eve.render +104 -0
  34. data/code/render/xhtml/amy.render +179 -0
  35. data/code/render/xhtml/blackboard.render +119 -0
  36. data/code/render/xhtml/brilliance_black.render +560 -0
  37. data/code/render/xhtml/brilliance_dull.render +569 -0
  38. data/code/render/xhtml/cobalt.render +170 -0
  39. data/code/render/xhtml/dawn.render +134 -0
  40. data/code/render/xhtml/eiffel.render +140 -0
  41. data/code/render/xhtml/espresso_libre.render +131 -0
  42. data/code/render/xhtml/files/css/active4d.css +114 -0
  43. data/code/render/xhtml/files/css/all_hallows_eve.css +72 -0
  44. data/code/render/xhtml/files/css/amy.css +147 -0
  45. data/code/render/xhtml/files/css/blackboard.css +88 -0
  46. data/code/render/xhtml/files/css/brilliance_black.css +605 -0
  47. data/code/render/xhtml/files/css/brilliance_dull.css +599 -0
  48. data/code/render/xhtml/files/css/cobalt.css +149 -0
  49. data/code/render/xhtml/files/css/dawn.css +121 -0
  50. data/code/render/xhtml/files/css/eiffel.css +121 -0
  51. data/code/render/xhtml/files/css/espresso_libre.css +109 -0
  52. data/code/render/xhtml/files/css/idle.css +62 -0
  53. data/code/render/xhtml/files/css/iplastic.css +80 -0
  54. data/code/render/xhtml/files/css/lazy.css +73 -0
  55. data/code/render/xhtml/files/css/mac_classic.css +123 -0
  56. data/code/render/xhtml/files/css/magicwb_amiga.css +104 -0
  57. data/code/render/xhtml/files/css/pastels_on_dark.css +188 -0
  58. data/code/render/xhtml/files/css/slush_poppies.css +85 -0
  59. data/code/render/xhtml/files/css/spacecadet.css +51 -0
  60. data/code/render/xhtml/files/css/sunburst.css +180 -0
  61. data/code/render/xhtml/files/css/twilight.css +137 -0
  62. data/code/render/xhtml/files/css/zenburnesque.css +91 -0
  63. data/code/render/xhtml/idle.render +101 -0
  64. data/code/render/xhtml/iplastic.render +107 -0
  65. data/code/render/xhtml/lazy.render +104 -0
  66. data/code/render/xhtml/mac_classic.render +143 -0
  67. data/code/render/xhtml/magicwb_amiga.render +125 -0
  68. data/code/render/xhtml/pastels_on_dark.render +212 -0
  69. data/code/render/xhtml/slush_poppies.render +131 -0
  70. data/code/render/xhtml/spacecadet.render +89 -0
  71. data/code/render/xhtml/sunburst.render +194 -0
  72. data/code/render/xhtml/twilight.render +161 -0
  73. data/code/render/xhtml/zenburnesque.render +134 -0
  74. data/code/syntax/actionscript.syntax +97 -0
  75. data/code/syntax/active4d.syntax +276 -0
  76. data/code/syntax/active4d_html.syntax +311 -0
  77. data/code/syntax/active4d_ini.syntax +50 -0
  78. data/code/syntax/active4d_library.syntax +21 -0
  79. data/code/syntax/ada.syntax +33 -0
  80. data/code/syntax/antlr.syntax +151 -0
  81. data/code/syntax/apache.syntax +191 -0
  82. data/code/syntax/applescript.syntax +384 -0
  83. data/code/syntax/asp.syntax +70 -0
  84. data/code/syntax/asp_vb.net.syntax +129 -0
  85. data/code/syntax/bibtex.syntax +151 -0
  86. data/code/syntax/blog_html.syntax +41 -0
  87. data/code/syntax/blog_markdown.syntax +42 -0
  88. data/code/syntax/blog_text.syntax +27 -0
  89. data/code/syntax/blog_textile.syntax +27 -0
  90. data/code/syntax/build.syntax +53 -0
  91. data/code/syntax/bulletin_board.syntax +287 -0
  92. data/code/syntax/cake.syntax +55 -0
  93. data/code/syntax/camlp4.syntax +36 -0
  94. data/code/syntax/cm.syntax +32 -0
  95. data/code/syntax/coldfusion.syntax +119 -0
  96. data/code/syntax/context_free.syntax +176 -0
  97. data/code/syntax/cs.syntax +59 -0
  98. data/code/syntax/css.syntax +195 -0
  99. data/code/syntax/css_experimental.syntax +1925 -0
  100. data/code/syntax/csv.syntax +68 -0
  101. data/code/syntax/d.syntax +142 -0
  102. data/code/syntax/diff.syntax +81 -0
  103. data/code/syntax/dokuwiki.syntax +204 -0
  104. data/code/syntax/dot.syntax +47 -0
  105. data/code/syntax/doxygen.syntax +43 -0
  106. data/code/syntax/dylan.syntax +62 -0
  107. data/code/syntax/eiffel.syntax +78 -0
  108. data/code/syntax/erlang.syntax +922 -0
  109. data/code/syntax/f-script.syntax +80 -0
  110. data/code/syntax/fortran.syntax +141 -0
  111. data/code/syntax/fxscript.syntax +142 -0
  112. data/code/syntax/greasemonkey.syntax +34 -0
  113. data/code/syntax/gri.syntax +83 -0
  114. data/code/syntax/groovy.syntax +191 -0
  115. data/code/syntax/gtd.syntax +22 -0
  116. data/code/syntax/gtdalt.syntax +143 -0
  117. data/code/syntax/haml.syntax +88 -0
  118. data/code/syntax/haskell.syntax +88 -0
  119. data/code/syntax/html-asp.syntax +27 -0
  120. data/code/syntax/html.syntax +362 -0
  121. data/code/syntax/html_django.syntax +36 -0
  122. data/code/syntax/html_for_asp.net.syntax +424 -0
  123. data/code/syntax/html_mason.syntax +119 -0
  124. data/code/syntax/html_rails.syntax +40 -0
  125. data/code/syntax/html_tcl.syntax +26 -0
  126. data/code/syntax/icalendar.syntax +32 -0
  127. data/code/syntax/inform.syntax +48 -0
  128. data/code/syntax/ini.syntax +55 -0
  129. data/code/syntax/installer_distribution_script.syntax +77 -0
  130. data/code/syntax/io.syntax +81 -0
  131. data/code/syntax/java.syntax +211 -0
  132. data/code/syntax/javaproperties.syntax +20 -0
  133. data/code/syntax/javascript.syntax +256 -0
  134. data/code/syntax/javascript_+_prototype.syntax +72 -0
  135. data/code/syntax/javascript_+_prototype_bracketed.syntax +140 -0
  136. data/code/syntax/jquery_javascript.syntax +114 -0
  137. data/code/syntax/json.syntax +136 -0
  138. data/code/syntax/languagedefinition.syntax +708 -0
  139. data/code/syntax/latex.syntax +566 -0
  140. data/code/syntax/latex_beamer.syntax +41 -0
  141. data/code/syntax/latex_log.syntax +50 -0
  142. data/code/syntax/latex_memoir.syntax +64 -0
  143. data/code/syntax/lexflex.syntax +219 -0
  144. data/code/syntax/lighttpd.syntax +54 -0
  145. data/code/syntax/lilypond.syntax +492 -0
  146. data/code/syntax/lisp.syntax +61 -0
  147. data/code/syntax/literate_haskell.syntax +24 -0
  148. data/code/syntax/logo.syntax +29 -0
  149. data/code/syntax/logtalk.syntax +152 -0
  150. data/code/syntax/lua.syntax +86 -0
  151. data/code/syntax/m.syntax +142 -0
  152. data/code/syntax/macports_portfile.syntax +163 -0
  153. data/code/syntax/makefile.syntax +36 -0
  154. data/code/syntax/man.syntax +17 -0
  155. data/code/syntax/mediawiki.syntax +567 -0
  156. data/code/syntax/mips.syntax +66 -0
  157. data/code/syntax/mod_perl.syntax +50 -0
  158. data/code/syntax/modula-3.syntax +47 -0
  159. data/code/syntax/moinmoin.syntax +189 -0
  160. data/code/syntax/mootools.syntax +572 -0
  161. data/code/syntax/movable_type.syntax +162 -0
  162. data/code/syntax/multimarkdown.syntax +39 -0
  163. data/code/syntax/objective-c++.syntax +18 -0
  164. data/code/syntax/objective-c.syntax +233 -0
  165. data/code/syntax/ocaml.syntax +764 -0
  166. data/code/syntax/ocamllex.syntax +167 -0
  167. data/code/syntax/ocamlyacc.syntax +184 -0
  168. data/code/syntax/opengl.syntax +14 -0
  169. data/code/syntax/pascal.syntax +77 -0
  170. data/code/syntax/perl.syntax +1115 -0
  171. data/code/syntax/plain_text.syntax +32 -0
  172. data/code/syntax/postscript.syntax +114 -0
  173. data/code/syntax/processing.syntax +106 -0
  174. data/code/syntax/prolog.syntax +40 -0
  175. data/code/syntax/property_list.syntax +635 -0
  176. data/code/syntax/python.syntax +868 -0
  177. data/code/syntax/python_django.syntax +21 -0
  178. data/code/syntax/qmake_project.syntax +114 -0
  179. data/code/syntax/qt_c++.syntax +26 -0
  180. data/code/syntax/quake3_config.syntax +32 -0
  181. data/code/syntax/r.syntax +81 -0
  182. data/code/syntax/r_console.syntax +16 -0
  183. data/code/syntax/ragel.syntax +201 -0
  184. data/code/syntax/rd_r_documentation.syntax +91 -0
  185. data/code/syntax/regexp.syntax +50 -0
  186. data/code/syntax/release_notes.syntax +46 -0
  187. data/code/syntax/remind.syntax +253 -0
  188. data/code/syntax/restructuredtext.syntax +250 -0
  189. data/code/syntax/rez.syntax +80 -0
  190. data/code/syntax/ruby.syntax +1035 -0
  191. data/code/syntax/ruby_experimental.syntax +145 -0
  192. data/code/syntax/ruby_on_rails.syntax +88 -0
  193. data/code/syntax/s5.syntax +69 -0
  194. data/code/syntax/scheme.syntax +347 -0
  195. data/code/syntax/scilab.syntax +41 -0
  196. data/code/syntax/setext.syntax +147 -0
  197. data/code/syntax/shell-unix-generic.syntax +384 -0
  198. data/code/syntax/slate.syntax +149 -0
  199. data/code/syntax/sql.syntax +237 -0
  200. data/code/syntax/sql_rails.syntax +18 -0
  201. data/code/syntax/ssh-config.syntax +33 -0
  202. data/code/syntax/standard_ml.syntax +121 -0
  203. data/code/syntax/strings_file.syntax +39 -0
  204. data/code/syntax/subversion_commit_message.syntax +36 -0
  205. data/code/syntax/sweave.syntax +84 -0
  206. data/code/syntax/swig.syntax +57 -0
  207. data/code/syntax/tcl.syntax +152 -0
  208. data/code/syntax/template_toolkit.syntax +121 -0
  209. data/code/syntax/tex.syntax +86 -0
  210. data/code/syntax/tex_math.syntax +49 -0
  211. data/code/syntax/textile.syntax +215 -0
  212. data/code/syntax/tsv.syntax +50 -0
  213. data/code/syntax/twiki.syntax +241 -0
  214. data/code/syntax/txt2tags.syntax +79 -0
  215. data/code/syntax/vectorscript.syntax +57 -0
  216. data/code/syntax/xhtml_1.0.syntax +4027 -0
  217. data/code/syntax/xml.syntax +180 -0
  218. data/code/syntax/xml_strict.syntax +92 -0
  219. data/code/syntax/xsl.syntax +60 -0
  220. data/code/syntax/yaml.syntax +160 -0
  221. data/code/syntax/yui_javascript.syntax +176 -0
  222. data/lib/bayeux/bayeux.rb +727 -0
  223. data/lib/bayeux/html_gen.rb +506 -0
  224. data/lib/bayeux/latex_gen.rb +333 -0
  225. data/lib/bayeux/para_block.rb +201 -0
  226. data/lib/bayeux/text_gen.rb +307 -0
  227. data/lib/bayeux/tree_walker.rb +166 -0
  228. data/lib/filters/bayeux.rb +44 -0
  229. data/lib/filters/pandoc.rb +23 -0
  230. data/lib/filters/section_id.rb +105 -0
  231. data/lib/helpers/breadcrumbs.rb +64 -0
  232. data/lib/helpers/site_toc.rb +11 -0
  233. data/lib/lib/uv.rb +92 -0
  234. data/lib/lib/uv/render_processor.rb +146 -0
  235. data/lib/lib/uv/utility.rb +67 -0
  236. data/lib/refs/Doc_Blocks.rb +2 -0
  237. data/lib/refs/References.rb +104 -0
  238. metadata +415 -0
@@ -0,0 +1,333 @@
1
+ class BayeuxLatexGen
2
+
3
+ # Default Constructor
4
+ def initialize(syntax_tree)
5
+ @syntax_tree = syntax_tree
6
+ @lines = Array.new
7
+ @text = String.new
8
+ end
9
+
10
+ # Return the AST as an HTML string (stored in @lines)
11
+ def generate
12
+
13
+ # Create a walker for the trees in the forest
14
+ walker = TreeWalker.new
15
+
16
+ walker.on_before_down = self.method(:generate_node_start)
17
+ walker.on_after_up = self.method(:generate_node_end)
18
+
19
+ walker.on_no_siblings = self.method(:generate_full_node)
20
+ walker.on_no_children = self.method(:generate_full_node)
21
+
22
+ # Clear the internal LaTeX replacement
23
+ @lines.clear
24
+
25
+ # Walk the forest
26
+ @syntax_tree.block_forest.each{|tree|
27
+ walker.walk_tree(tree)
28
+ }
29
+
30
+ #puts @syntax_tree.to_s
31
+ #puts @lines
32
+
33
+ latex_string = String.new
34
+ @lines.each{|line|
35
+ unless (line.empty? or (/\S/ !~ line)) then
36
+ #unless line.empty? then
37
+ latex_string << line + "\n"
38
+ end
39
+ }
40
+
41
+ return latex_string
42
+ end
43
+
44
+ ###
45
+ ### Walker Hooks. These are called by the tree walker on specific
46
+ ### events
47
+ ###
48
+
49
+ # Output the full contents of the node, properly bracketed as an
50
+ # LaTeX expression. This is only called if we have no sub-nodes to
51
+ # deal with
52
+ def generate_full_node(block)
53
+
54
+ # Check if we need to add a space
55
+ # Check if we need to add a space
56
+ if not block.content[0] =~ /\s|[:;.,?!]/
57
+ @lines << " "
58
+ end
59
+
60
+ case block.type
61
+
62
+ # Headers
63
+ when :h1
64
+ @lines << "\\chapter{#{block.content}}\n"
65
+ when :h2
66
+ @lines << "\\section{#{block.content}}\n"
67
+ when :h3
68
+ @lines << "\\subsection{#{block.content}}\n"
69
+ when :h4
70
+ @lines << "\\subsubsection{#{block.content}}\n"
71
+ when :h5
72
+ @lines << "\\paragraph{#{block.content}}\n"
73
+ when :h5
74
+ @lines << "\\subparagraph{#{block.content}}\n"
75
+
76
+ # Ordinary paragraphs
77
+ when :paragraph
78
+ @lines << "#{typeset(block.content)}\n"
79
+
80
+ when :none
81
+ @text << "#{block.content}"
82
+
83
+ # Special paragraphs
84
+ when :block_quote
85
+ @lines << "\\begin{quotation}" << "#{typeset(block.content)}" << "\\end{quotation}\n"
86
+ when :single_quote
87
+ @lines << "'#{typeset(block.content)}'"
88
+ when :double_quote
89
+ @lines << "\"#{typeset(block.content)}\""
90
+
91
+ when :note
92
+ @lines << "\\textbf{Note:}~#{typeset(block.content)}\n"
93
+
94
+ when :command
95
+ @lines << "\\begin{Verbatim}[frame=lines,rulecolor=\\color{highlight}]" << "#{block.content.strip.indent(3)}" << "\\end{Verbatim}\n"
96
+
97
+ when :code_language
98
+ @code_language = block.content
99
+ when :code_start_number
100
+ @code_start_number = block.content
101
+
102
+ when :file
103
+ @lines << "<pre class=\"file\">#{block.content}</pre>"
104
+ when :output
105
+ @lines << "\\crule" << "\\begin{verbatim}" << "#{block.content.strip}" << "\\end{verbatim}" << "\\crule\n"
106
+
107
+ # Special Characters
108
+ when :em_dash
109
+ @text << "---"
110
+ when :en_dash
111
+ @text << "--"
112
+ when :elipses
113
+ @text << "\\ldots"
114
+ when :elipses_stop
115
+ @text << "\\dots."
116
+
117
+ # Links
118
+ when :link_target
119
+ @link_target = block.content
120
+ when :link_text
121
+ @link_text = block.content
122
+
123
+ # Lists
124
+ when :item
125
+ @lines << "\\item #{typeset(block.content)}"
126
+
127
+ when :dl_header
128
+ @lines << "\\item[#{block.content}]"
129
+ when :dl_text
130
+ @lines << "#{block.content}"
131
+
132
+ # Tags
133
+ when :ac
134
+ @text << "\\ac{#{block.content}}"
135
+
136
+ when :emph
137
+ @text << "\\emph{#{block.content}}"
138
+
139
+ when :tt
140
+ @text << "\\texttt{#{block.content}}"
141
+
142
+ end
143
+ end
144
+
145
+ # Output only the start of a node
146
+ def generate_node_start(block)
147
+
148
+ # Check if we need to add a space
149
+ if not block.content[0] =~ /\s|[:;.,?!]/
150
+ @lines << " "
151
+ end
152
+
153
+ case block.type
154
+
155
+ # Headers
156
+ when :h1
157
+ @text << "\\chapter{#{block.content}"
158
+ when :h2
159
+ @text << "\\section{#{block.content}"
160
+ when :h3
161
+ @text << "\\subsection{#{block.content}"
162
+ when :h4
163
+ @text << "\\subsubsection{#{block.content}"
164
+ when :h5
165
+ @text << "\\paragraph{#{block.content}"
166
+ when :h5
167
+ @text << "\\subparagraph{#{block.content}"
168
+
169
+ # Ordinary paragraphs
170
+ when :paragraph
171
+ @text << "#{block.content}"
172
+
173
+ when :none
174
+ @text << "#{block.content}"
175
+
176
+ # Special paragraphs
177
+ when :block_quote
178
+ @lines << "<blockquote>#{typeset(block.content)}"
179
+ when :single_quote
180
+ @text << "'#{block.content}"
181
+ when :double_quote
182
+ @text << "\"{block.content}"
183
+
184
+ when :note
185
+ @lines << "\\textbf{Note:}~#{typeset(block.content)}\n"
186
+
187
+ when :command
188
+ @lines << "\\begin{Verbatim}[frame=lines,rulecolor=\\color{highlight}]" << "#{typeset(block.content)}"
189
+
190
+ when :code_language
191
+ @code_language = block.content
192
+ when :code_start_number
193
+ @code_start_number = block.content
194
+
195
+ # Special Characters
196
+ when :em_dash
197
+ @text << "---"
198
+ when :en_dash
199
+ @text << "--"
200
+ when :elipses
201
+ @text << "\ldots"
202
+ when :elipses_stop
203
+ @text << "\dots."
204
+
205
+ # Links
206
+ when :link_target
207
+ @link_target = block.content
208
+ when :link_text
209
+ @link_text = block.content
210
+
211
+ # Lists
212
+ when :ol
213
+ @lines << "\\begin{enumerate}"
214
+ when :ul
215
+ @lines << "\\begin{itemize}"
216
+
217
+ when :dl
218
+ @lines << "\\begin{description}"
219
+ when :dl_header
220
+ @lines << "\\item[#{block.content}"
221
+ when :dl_text
222
+ @lines << "#{block.content}"
223
+
224
+ # Tags
225
+ when :ac
226
+ @text << "\\ac{#{block.content}}"
227
+
228
+ when :emph
229
+ @text << "\\emph{#{block.content}\\/}"
230
+
231
+ when :strong
232
+ @text << "\\texttt{#{block.content}\\/}"
233
+
234
+ when :tt
235
+ @text << "\\texttt{#{block.content}}"
236
+
237
+ end
238
+ end
239
+
240
+ def generate_node_end(block)
241
+
242
+ case block.type
243
+
244
+ # Headers
245
+ when :h1, :h2, :h3, :h4, :h5, :h6
246
+ @lines << "#{typeset(@text)}}\n"
247
+ @text = ""
248
+
249
+ # Ordinary paragraphs
250
+ when :paragraph
251
+ @lines << "#{typeset(@text)}\n"
252
+ @text = ""
253
+
254
+ # Special paragraphs
255
+ when :block_quote
256
+ @lines << "\\end{quotation}\n"
257
+ when :single_quote
258
+ @lines << "'"
259
+ when :double_quote
260
+ @lines << "\""
261
+
262
+ when :note
263
+ @lines << ""
264
+
265
+ when :command
266
+ @lines << "\\end{Verbatim}\n"
267
+
268
+ when :code
269
+ # Assemble the code block from the sub-tree nodes
270
+ begin
271
+ pretty_code = Uv.parse(block.content, "xhtml", @code_language, @code_start_number.to_i, "dawn")
272
+ @lines << pretty_code
273
+ rescue
274
+ pretty_code = Uv.parse(block.content, "xhtml", "plain_text", @code_start_number.to_i, "dawn")
275
+ @lines << pretty_code
276
+ end
277
+
278
+ # Links
279
+ when :link
280
+ # Assemble the link from the sub-tree nodes
281
+ @lines << "\\href{#{@link_target}\"}{#{@link_text}}"
282
+ when :link_target
283
+ @link_target = block.content
284
+ when :link_text
285
+ @link_text = block.content
286
+
287
+ # Lists
288
+ when :ol
289
+ @lines << "\\end{enumerate}"
290
+ when :ul
291
+ @lines << "\\end{itemize}"
292
+ when :item
293
+ @lines << "\\item #{typeset(@text)}"
294
+ @text = ""
295
+
296
+ when :dl
297
+ @lines << ""
298
+ when :dl_header
299
+ @lines << "]"
300
+ when :dl_text
301
+ @lines << ""
302
+
303
+ # Tags
304
+ when :ac, :tt, :strong
305
+ @lines << "}"
306
+
307
+ when :emph
308
+ @lines << "\\/}"
309
+
310
+ end
311
+ end
312
+
313
+ ###
314
+ ### Helper functions
315
+ ###
316
+
317
+
318
+ # Fix Typography, according to HTML standards
319
+ def typeset(string)
320
+ unless string.nil? then
321
+ return_str = string
322
+
323
+ return_str = return_str.strip
324
+ return_str = return_str.squeeze(" ")
325
+ return_str = return_str.word_wrap(70)
326
+
327
+ return return_str.rstrip
328
+ else
329
+ return ""
330
+ end
331
+ end
332
+
333
+ end
@@ -0,0 +1,201 @@
1
+ class ParaBlock
2
+
3
+ # Default contructor
4
+ def initialize(type, content = "", target = nil)
5
+ @orig_type = type
6
+ @type = normalise_type(type)
7
+
8
+ @content = content
9
+ @target = target
10
+ end
11
+
12
+ ###
13
+ ### Accessors
14
+ ###
15
+
16
+ def type=(type)
17
+ @type = normalise_type(type)
18
+ end
19
+ def type
20
+ @type
21
+ end
22
+
23
+ def content=(content)
24
+ @content = content
25
+ end
26
+ def content
27
+ @content
28
+ end
29
+
30
+ def target=(target)
31
+ @target = target
32
+ end
33
+ def target
34
+ @target
35
+ end
36
+
37
+ ###
38
+ ### Convenience functions
39
+ ###
40
+
41
+ def <<(value)
42
+ @content << value
43
+ end
44
+
45
+ def empty?
46
+ return (@content.empty? or (/\S/ !~ @content))
47
+ end
48
+
49
+ def clear
50
+ @content.clear
51
+ end
52
+
53
+ # Returns the length of the type and target, as they were
54
+ # written out _in the original document_. For the current
55
+ # type length, see type_length
56
+ def orig_type_length
57
+ @orig_type.to_s.length + @target.to_s.length + 1
58
+ end
59
+
60
+ # Returns the length of the type and target, as they would
61
+ # be written out (including the separator), e.g as
62
+ # 'figure:L3'.length
63
+ def type_length
64
+ @type.to_s.length + @target.to_s.length + 1
65
+ end
66
+
67
+ # Take the current contents as a block
68
+ # type, and reset the content
69
+ def content_to_type!
70
+
71
+ unless @content =~ /\s+/ then
72
+ header = @content.split(':')
73
+
74
+ if header[0].nil? then
75
+
76
+ # If we can't work out the type, set everything
77
+ # to null
78
+ @type = :none
79
+ @target = nil
80
+
81
+ else
82
+
83
+ # Attempt to coerce the content to a type
84
+ block_type = header[0].to_sym
85
+ unless block_type.nil? then
86
+ @orig_type = block_type
87
+ @type = normalise_type(block_type)
88
+ else
89
+ @orig_type = :none
90
+ @type = :none
91
+ end
92
+
93
+ @target = header[1]
94
+
95
+ end
96
+
97
+ else
98
+ @type = :none
99
+ end
100
+
101
+ @content.clear
102
+ end
103
+
104
+ ###
105
+ ### Helper functions
106
+ ###
107
+
108
+ # Do type mapping if needed, otherwise
109
+ # pass on the contents as the type
110
+ def normalise_type(type)
111
+ case type
112
+ when :e
113
+ return :emph
114
+ when :s
115
+ return :strong
116
+ when :quote
117
+ return :block_quote
118
+ else
119
+ return type
120
+ end
121
+ end
122
+
123
+ ###
124
+ ### Type conversions
125
+ ###
126
+
127
+ def self.from_s(string)
128
+
129
+ unless string =~ /\s+/ then
130
+ header = string.split(':')
131
+
132
+ if header[0].nil? then
133
+
134
+ # If we can't work out the type, set everything
135
+ # to null
136
+ type = :none
137
+ target = nil
138
+
139
+ else
140
+
141
+ # Attempt to coerce the content to a type
142
+ block_type = header[0].to_sym
143
+ unless block_type.nil? then
144
+ type = block_type
145
+ else
146
+ type = :none
147
+ end
148
+
149
+ target = header[1]
150
+
151
+ end
152
+
153
+ else
154
+ type = :none
155
+ end
156
+
157
+ return ParaBlock.new(type, "", target)
158
+ end
159
+
160
+ def to_s
161
+ @content.to_s
162
+ end
163
+
164
+ def to_debug_s
165
+ return "block type: #{type}, content: #{content}"
166
+ end
167
+
168
+ def to_sym
169
+ @content.to_sym
170
+ end
171
+
172
+ def to_json(*a)
173
+ begin
174
+ require 'json'
175
+
176
+ json_hash = {
177
+ "type" => @name,
178
+ "content" => @content,
179
+ "target" => @target,
180
+ JSON.create_id => self.class.name
181
+ }
182
+
183
+ return json_hash.to_json
184
+
185
+ rescue LoadError
186
+ warn "The JSON gem couldn't be loaded, and so the JSON representation could not be generated"
187
+ end
188
+ end
189
+
190
+ def self.json_create(json_hash)
191
+ begin
192
+ require 'json'
193
+
194
+ block = new(json_hash["type"], json_hash["content"], json_hash["target"])
195
+ return block
196
+
197
+ rescue LoadError
198
+ warn "The JSON gem couldn't be loaded, and so the JSON representation could not be generated"
199
+ end
200
+ end
201
+ end