From 0b7aff8bd1ee47f33cf8ffb4685d0b9480ab4cd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Teichgr=C3=A4ber?= Date: Thu, 26 Apr 2012 20:39:43 +0200 Subject: [PATCH] output.go: reduce verbosity of `obfuscate' feature --- output.go | 67 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/output.go b/output.go index a106a9c..42c0289 100644 --- a/output.go +++ b/output.go @@ -35,6 +35,7 @@ type Writer interface { type htmlOut struct { Writer padded int + obfuscate bool notenum int endNotes []*element /* List of endnotes to print after main content. */ @@ -46,7 +47,7 @@ func (d *Doc) WriteHtml(w Writer) int { out := new(htmlOut) out.Writer = w out.padded = 2 - out.elist(d.tree, false) + out.elist(d.tree) if len(out.endNotes) != 0 { out.pad(2) out.printEndnotes() @@ -78,7 +79,7 @@ func (w *htmlOut) s(s string) *htmlOut { /* print string, escaping for HTML * If obfuscate selected, convert characters to hex or decimal entities at random */ -func (w *htmlOut) str(s string, obfuscate bool) *htmlOut { +func (w *htmlOut) str(s string) *htmlOut { var ws string var i0 = 0 @@ -93,7 +94,7 @@ func (w *htmlOut) str(s string, obfuscate bool) *htmlOut { case '"': ws = """ default: - if obfuscate { + if w.obfuscate { if rand.Intn(1) == 0 { ws = fmt.Sprintf("&#%d;", r) } else { @@ -120,16 +121,16 @@ func (w *htmlOut) str(s string, obfuscate bool) *htmlOut { /* print a list of elements */ -func (w *htmlOut) elist(list *element, obfuscate bool) *htmlOut { +func (w *htmlOut) elist(list *element) *htmlOut { for list != nil { - w.elem(list, obfuscate) + w.elem(list) list = list.next } return w } // print an element -func (w *htmlOut) elem(elt *element, obfuscate bool) *htmlOut { +func (w *htmlOut) elem(elt *element) *htmlOut { var s string switch elt.key { @@ -138,7 +139,7 @@ func (w *htmlOut) elem(elt *element, obfuscate bool) *htmlOut { case LINEBREAK: s = "
\n" case STR: - w.str(elt.contents.str, obfuscate) + w.str(elt.contents.str) case ELLIPSIS: s = "…" case EMDASH: @@ -148,65 +149,67 @@ func (w *htmlOut) elem(elt *element, obfuscate bool) *htmlOut { case APOSTROPHE: s = "’" case SINGLEQUOTED: - w.s("‘").elist(elt.children, obfuscate).s("’") + w.s("‘").elist(elt.children).s("’") case DOUBLEQUOTED: - w.s("“").elist(elt.children, obfuscate).s("”") + w.s("“").elist(elt.children).s("”") case CODE: - w.s("").str(elt.contents.str, obfuscate).s("") + w.s("").str(elt.contents.str).s("") case HTML: s = elt.contents.str case LINK: + o := w.obfuscate if strings.Index(elt.contents.link.url, "mailto:") == 0 { - obfuscate = true /* obfuscate mailto: links */ + w.obfuscate = true /* obfuscate mailto: links */ } - w.s(` 0 { - w.s(` title="`).str(elt.contents.link.title, obfuscate).s(`"`) + w.s(` title="`).str(elt.contents.link.title).s(`"`) } - w.s(">").elist(elt.contents.link.label, obfuscate).s("") + w.s(">").elist(elt.contents.link.label).s("") + w.obfuscate = o case IMAGE: - w.s(``)
-		w.elist(elt.contents.link.label, obfuscate).s(` 0 { - w.s(` title="`).str(elt.contents.link.title, obfuscate).s(`"`) + w.s(` title="`).str(elt.contents.link.title).s(`"`) } w.s(" />") case EMPH: - w.s("").elist(elt.children, obfuscate).s("") + w.s("").elist(elt.children).s("") case STRONG: - w.s("").elist(elt.children, obfuscate).s("") + w.s("").elist(elt.children).s("") case LIST: - w.elist(elt.children, obfuscate) + w.elist(elt.children) case RAW: /* Shouldn't occur - these are handled by process_raw_blocks() */ log.Fatalf("RAW") case H1, H2, H3, H4, H5, H6: h := "h" + string('1'+elt.key-H1) + ">" /* assumes H1 ... H6 are in order */ - w.pad(2).s("<").s(h).elist(elt.children, obfuscate).s("").elist(elt.children, obfuscate).s("

").pset(0) + w.pad(2).s("

").elist(elt.children).s("

").pset(0) case HRULE: w.pad(2).s("
").pset(0) case HTMLBLOCK: w.pad(2).s(elt.contents.str).pset(0) case VERBATIM: - w.pad(2).s("
").str(elt.contents.str, obfuscate).s("
").pset(0) + w.pad(2).s("
").str(elt.contents.str).s("
").pset(0) case BULLETLIST: - w.pad(2).s("").pset(0) + w.pad(2).s("").pset(0) case ORDEREDLIST: - w.pad(2).s("
    ").pset(0).elist(elt.children, obfuscate).pad(1).s("
").pset(0) + w.pad(2).s("
    ").pset(0).elist(elt.children).pad(1).s("
").pset(0) case DEFINITIONLIST: - w.pad(2).s("
").pset(0).elist(elt.children, obfuscate).pad(1).s("
").pset(0) + w.pad(2).s("
").pset(0).elist(elt.children).pad(1).s("
").pset(0) case DEFTITLE: - w.pad(1).s("
").pset(2).elist(elt.children, obfuscate).s("
").pset(0) + w.pad(1).s("
").pset(2).elist(elt.children).s("
").pset(0) case DEFDATA: - w.pad(1).s("
").pset(2).elist(elt.children, obfuscate).s("
").pset(0) + w.pad(1).s("
").pset(2).elist(elt.children).s("
").pset(0) case LISTITEM: - w.pad(1).s("
  • ").pset(2).elist(elt.children, obfuscate).s("
  • ").pset(0) + w.pad(1).s("
  • ").pset(2).elist(elt.children).s("
  • ").pset(0) case BLOCKQUOTE: - w.pad(2).s("
    \n").pset(2).elist(elt.children, obfuscate).pad(1).s("
    ").pset(0) + w.pad(2).s("
    \n").pset(2).elist(elt.children).pad(1).s("
    ").pset(0) case REFERENCE: /* Nonprinting */ case NOTE: @@ -236,7 +239,7 @@ func (w *htmlOut) printEndnotes() { for _, elt := range w.endNotes { counter++ w.pad(1).s(fmt.Sprintf("
  • \n", counter)).pset(2) - w.elist(elt.children, false) + w.elist(elt.children) w.s(fmt.Sprintf(" [back]", counter)) w.pad(1).s("
  • ") }