output.go: reduce verbosity of `obfuscate' feature

This commit is contained in:
Michael Teichgräber 2012-04-26 20:39:43 +02:00
parent 42e21972e8
commit 0b7aff8bd1

View File

@ -35,6 +35,7 @@ type Writer interface {
type htmlOut struct { type htmlOut struct {
Writer Writer
padded int padded int
obfuscate bool
notenum int notenum int
endNotes []*element /* List of endnotes to print after main content. */ 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 := new(htmlOut)
out.Writer = w out.Writer = w
out.padded = 2 out.padded = 2
out.elist(d.tree, false) out.elist(d.tree)
if len(out.endNotes) != 0 { if len(out.endNotes) != 0 {
out.pad(2) out.pad(2)
out.printEndnotes() out.printEndnotes()
@ -78,7 +79,7 @@ func (w *htmlOut) s(s string) *htmlOut {
/* print string, escaping for HTML /* print string, escaping for HTML
* If obfuscate selected, convert characters to hex or decimal entities at random * 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 ws string
var i0 = 0 var i0 = 0
@ -93,7 +94,7 @@ func (w *htmlOut) str(s string, obfuscate bool) *htmlOut {
case '"': case '"':
ws = """ ws = """
default: default:
if obfuscate { if w.obfuscate {
if rand.Intn(1) == 0 { if rand.Intn(1) == 0 {
ws = fmt.Sprintf("&#%d;", r) ws = fmt.Sprintf("&#%d;", r)
} else { } else {
@ -120,16 +121,16 @@ func (w *htmlOut) str(s string, obfuscate bool) *htmlOut {
/* print a list of elements /* print a list of elements
*/ */
func (w *htmlOut) elist(list *element, obfuscate bool) *htmlOut { func (w *htmlOut) elist(list *element) *htmlOut {
for list != nil { for list != nil {
w.elem(list, obfuscate) w.elem(list)
list = list.next list = list.next
} }
return w return w
} }
// print an element // print an element
func (w *htmlOut) elem(elt *element, obfuscate bool) *htmlOut { func (w *htmlOut) elem(elt *element) *htmlOut {
var s string var s string
switch elt.key { switch elt.key {
@ -138,7 +139,7 @@ func (w *htmlOut) elem(elt *element, obfuscate bool) *htmlOut {
case LINEBREAK: case LINEBREAK:
s = "<br/>\n" s = "<br/>\n"
case STR: case STR:
w.str(elt.contents.str, obfuscate) w.str(elt.contents.str)
case ELLIPSIS: case ELLIPSIS:
s = "&hellip;" s = "&hellip;"
case EMDASH: case EMDASH:
@ -148,65 +149,67 @@ func (w *htmlOut) elem(elt *element, obfuscate bool) *htmlOut {
case APOSTROPHE: case APOSTROPHE:
s = "&rsquo;" s = "&rsquo;"
case SINGLEQUOTED: case SINGLEQUOTED:
w.s("&lsquo;").elist(elt.children, obfuscate).s("&rsquo;") w.s("&lsquo;").elist(elt.children).s("&rsquo;")
case DOUBLEQUOTED: case DOUBLEQUOTED:
w.s("&ldquo;").elist(elt.children, obfuscate).s("&rdquo;") w.s("&ldquo;").elist(elt.children).s("&rdquo;")
case CODE: case CODE:
w.s("<code>").str(elt.contents.str, obfuscate).s("</code>") w.s("<code>").str(elt.contents.str).s("</code>")
case HTML: case HTML:
s = elt.contents.str s = elt.contents.str
case LINK: case LINK:
o := w.obfuscate
if strings.Index(elt.contents.link.url, "mailto:") == 0 { if strings.Index(elt.contents.link.url, "mailto:") == 0 {
obfuscate = true /* obfuscate mailto: links */ w.obfuscate = true /* obfuscate mailto: links */
} }
w.s(`<a href="`).str(elt.contents.link.url, obfuscate).s(`"`) w.s(`<a href="`).str(elt.contents.link.url).s(`"`)
if len(elt.contents.link.title) > 0 { if len(elt.contents.link.title) > 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("</a>") w.s(">").elist(elt.contents.link.label).s("</a>")
w.obfuscate = o
case IMAGE: case IMAGE:
w.s(`<img src="`).str(elt.contents.link.url, obfuscate).s(`" alt="`) w.s(`<img src="`).str(elt.contents.link.url).s(`" alt="`)
w.elist(elt.contents.link.label, obfuscate).s(`"`) w.elist(elt.contents.link.label).s(`"`)
if len(elt.contents.link.title) > 0 { if len(elt.contents.link.title) > 0 {
w.s(` title="`).str(elt.contents.link.title, obfuscate).s(`"`) w.s(` title="`).str(elt.contents.link.title).s(`"`)
} }
w.s(" />") w.s(" />")
case EMPH: case EMPH:
w.s("<em>").elist(elt.children, obfuscate).s("</em>") w.s("<em>").elist(elt.children).s("</em>")
case STRONG: case STRONG:
w.s("<strong>").elist(elt.children, obfuscate).s("</strong>") w.s("<strong>").elist(elt.children).s("</strong>")
case LIST: case LIST:
w.elist(elt.children, obfuscate) w.elist(elt.children)
case RAW: case RAW:
/* Shouldn't occur - these are handled by process_raw_blocks() */ /* Shouldn't occur - these are handled by process_raw_blocks() */
log.Fatalf("RAW") log.Fatalf("RAW")
case H1, H2, H3, H4, H5, H6: case H1, H2, H3, H4, H5, H6:
h := "h" + string('1'+elt.key-H1) + ">" /* assumes H1 ... H6 are in order */ h := "h" + string('1'+elt.key-H1) + ">" /* assumes H1 ... H6 are in order */
w.pad(2).s("<").s(h).elist(elt.children, obfuscate).s("</").s(h).pset(0) w.pad(2).s("<").s(h).elist(elt.children).s("</").s(h).pset(0)
case PLAIN: case PLAIN:
w.pad(1).elist(elt.children, obfuscate).pset(0) w.pad(1).elist(elt.children).pset(0)
case PARA: case PARA:
w.pad(2).s("<p>").elist(elt.children, obfuscate).s("</p>").pset(0) w.pad(2).s("<p>").elist(elt.children).s("</p>").pset(0)
case HRULE: case HRULE:
w.pad(2).s("<hr />").pset(0) w.pad(2).s("<hr />").pset(0)
case HTMLBLOCK: case HTMLBLOCK:
w.pad(2).s(elt.contents.str).pset(0) w.pad(2).s(elt.contents.str).pset(0)
case VERBATIM: case VERBATIM:
w.pad(2).s("<pre><code>").str(elt.contents.str, obfuscate).s("</code></pre>").pset(0) w.pad(2).s("<pre><code>").str(elt.contents.str).s("</code></pre>").pset(0)
case BULLETLIST: case BULLETLIST:
w.pad(2).s("<ul>").pset(0).elist(elt.children, obfuscate).pad(1).s("</ul>").pset(0) w.pad(2).s("<ul>").pset(0).elist(elt.children).pad(1).s("</ul>").pset(0)
case ORDEREDLIST: case ORDEREDLIST:
w.pad(2).s("<ol>").pset(0).elist(elt.children, obfuscate).pad(1).s("</ol>").pset(0) w.pad(2).s("<ol>").pset(0).elist(elt.children).pad(1).s("</ol>").pset(0)
case DEFINITIONLIST: case DEFINITIONLIST:
w.pad(2).s("<dl>").pset(0).elist(elt.children, obfuscate).pad(1).s("</dl>").pset(0) w.pad(2).s("<dl>").pset(0).elist(elt.children).pad(1).s("</dl>").pset(0)
case DEFTITLE: case DEFTITLE:
w.pad(1).s("<dt>").pset(2).elist(elt.children, obfuscate).s("</dt>").pset(0) w.pad(1).s("<dt>").pset(2).elist(elt.children).s("</dt>").pset(0)
case DEFDATA: case DEFDATA:
w.pad(1).s("<dd>").pset(2).elist(elt.children, obfuscate).s("</dd>").pset(0) w.pad(1).s("<dd>").pset(2).elist(elt.children).s("</dd>").pset(0)
case LISTITEM: case LISTITEM:
w.pad(1).s("<li>").pset(2).elist(elt.children, obfuscate).s("</li>").pset(0) w.pad(1).s("<li>").pset(2).elist(elt.children).s("</li>").pset(0)
case BLOCKQUOTE: case BLOCKQUOTE:
w.pad(2).s("<blockquote>\n").pset(2).elist(elt.children, obfuscate).pad(1).s("</blockquote>").pset(0) w.pad(2).s("<blockquote>\n").pset(2).elist(elt.children).pad(1).s("</blockquote>").pset(0)
case REFERENCE: case REFERENCE:
/* Nonprinting */ /* Nonprinting */
case NOTE: case NOTE:
@ -236,7 +239,7 @@ func (w *htmlOut) printEndnotes() {
for _, elt := range w.endNotes { for _, elt := range w.endNotes {
counter++ counter++
w.pad(1).s(fmt.Sprintf("<li id=\"fn%d\">\n", counter)).pset(2) w.pad(1).s(fmt.Sprintf("<li id=\"fn%d\">\n", counter)).pset(2)
w.elist(elt.children, false) w.elist(elt.children)
w.s(fmt.Sprintf(" <a href=\"#fnref%d\" title=\"Jump back to reference\">[back]</a>", counter)) w.s(fmt.Sprintf(" <a href=\"#fnref%d\" title=\"Jump back to reference\">[back]</a>", counter))
w.pad(1).s("</li>") w.pad(1).s("</li>")
} }