nitcc: tests script return non-zero on failure (print is not enough)
[nit.git] / lib / markdown2 / markdown_rendering.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Markdown document rendering
16 module markdown_rendering
17
18 import markdown_ast
19
20 # Common interface for all markdown renderer
21 interface MdRenderer
22 super MdVisitor
23
24 # Render `node`
25 fun render(node: MdNode): String is abstract
26 end
27
28 # A renderer that output raw text
29 class RawTextVisitor
30 super MdRenderer
31
32 # Text under construction
33 private var text: Buffer is noinit
34
35 redef fun render(node) do
36 text = new Buffer
37 enter_visit(node)
38 return text.to_s
39 end
40
41 # Append `string` to `text`
42 fun add(string: String) do text.append(string)
43
44 redef fun visit(node) do node.render_raw_text(self)
45 end
46
47 redef class MdNode
48
49 # Return `self` as raw text
50 fun raw_text: String do
51 var v = new RawTextVisitor
52 return v.render(self)
53 end
54
55 # Render `self` as raw text
56 fun render_raw_text(v: RawTextVisitor) do visit_all(v)
57 end
58
59 redef class MdCode
60 redef fun render_raw_text(v) do v.add literal
61 end
62
63 redef class MdLineBreak
64 redef fun render_raw_text(v) do v.add "\n"
65 end
66
67 redef class MdText
68 redef fun render_raw_text(v) do v.add literal
69 end