1 module hunt.markdown.renderer.html.HtmlWriter; 2 3 import hunt.markdown.internal.util.Escaping; 4 5 import hunt.Exceptions; 6 import hunt.util.Common; 7 import hunt.text.Common; 8 import hunt.collection.Collections; 9 import hunt.collection.Map; 10 import hunt.util.Appendable; 11 import hunt.markdown.internal.util.Common; 12 13 class HtmlWriter { 14 15 mixin(MakeGlobalVar!(Map!(string, string))("NO_ATTRIBUTES",`Collections.emptyMap!(string, string)()`)); 16 17 18 private Appendable buffer; 19 private char lastChar = 0; 20 21 this(Appendable o) { 22 this.buffer = o; 23 } 24 25 public void raw(string s) { 26 append(s); 27 } 28 29 public void text(string text) { 30 append(Escaping.escapeHtml(text, false)); 31 } 32 33 public void tag(string name) { 34 tag(name, NO_ATTRIBUTES); 35 } 36 37 public void tag(string name, Map!(string, string) attrs) { 38 tag(name, attrs, false); 39 } 40 41 public void tag(string name, Map!(string, string) attrs, bool voidElement) { 42 append("<"); 43 append(name); 44 if (attrs !is null && !attrs.isEmpty()) { 45 foreach (string k ,string v ; attrs) { 46 append(" "); 47 append(Escaping.escapeHtml(k, true)); 48 append("=\""); 49 append(Escaping.escapeHtml(v, true)); 50 append("\""); 51 } 52 } 53 if (voidElement) { 54 append(" /"); 55 } 56 57 append(">"); 58 } 59 60 public void line() { 61 if (lastChar != 0 && lastChar != '\n') { 62 append("\n"); 63 } 64 } 65 66 protected void append(string s) { 67 try { 68 buffer.append(s); 69 } catch (IOException e) { 70 throw new RuntimeException(e); 71 } 72 int length = cast(int)(s.length); 73 if (length != 0) { 74 lastChar = s.charAt(length - 1); 75 } 76 } 77 }