1 module hunt.markdown.renderer.text.TextContentWriter;
2 
3 import hunt.Exceptions;
4 
5 import hunt.util.Appendable;
6 import hunt.util.Common;
7 import hunt.text.Common;
8 import std.regex;
9 
10 class TextContentWriter {
11 
12     private Appendable buffer;
13 
14     private char lastChar;
15 
16     public this(Appendable o) {
17         buffer = o;
18     }
19 
20     public void whitespace() {
21         if (lastChar != 0 && lastChar != ' ') {
22             append(' ');
23         }
24     }
25 
26     public void colon() {
27         if (lastChar != 0 && lastChar != ':') {
28             append(':');
29         }
30     }
31 
32     public void line() {
33         if (lastChar != 0 && lastChar != '\n') {
34             append('\n');
35         }
36     }
37 
38     public void writeStripped(string s) {
39         append(s.replaceAll(regex("[\\r\\n\\s]+"), " "));
40     }
41 
42     public void write(string s) {
43         append(s);
44     }
45 
46     public void write(char c) {
47         append(c);
48     }
49 
50     private void append(string s) {
51         try {
52             buffer.append(s);
53         } catch (IOException e) {
54             throw new RuntimeException(e);
55         }
56 
57         int length = cast(int)(s.length);
58         if (length != 0) {
59             lastChar = s.charAt(length - 1);
60         }
61     }
62 
63     private void append(char c) {
64         try {
65             buffer.append(c);
66         } catch (IOException e) {
67             throw new RuntimeException(e);
68         }
69 
70         lastChar = c;
71     }
72 }