Skip to content

Commit

Permalink
Maintain paragraphing, remove escaped chars
Browse files Browse the repository at this point in the history
Maintains the original paragraph strucures
Handles escaped chars e.g emojis
  • Loading branch information
matt-farmer committed Apr 16, 2018
1 parent 0ca8e58 commit c9c929e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
File renamed without changes.
19 changes: 17 additions & 2 deletions nap-writing-print/html-sanitizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package nap_writing_print

import (
"context"
"html"
"strings"

"github.com/microcosm-cc/bluemonday"
)
Expand All @@ -25,8 +27,21 @@ func createHtmlSanitizer(ctx context.Context, in <-chan map[string]string) (
rmap := rMap

p := bluemonday.StrictPolicy()
html := p.Sanitize(rmap["Item Response"])
rmap["Item Response"] = html
// keep paragraph markers to preserve layout in pdf printing
p.AllowElements("p")
sanitizedHtml := p.Sanitize(rmap["Item Response"])
// now unescape the html for speech marks, apostrophe's etc. for printing
unescapedHtml := html.UnescapeString(sanitizedHtml)

// remove unnecessary characters
// remove non-breaking spaces and line feeds & backticks
noBrHtml := strings.Replace(unescapedHtml, "&nbsp;", " ", -1)
noLfHtml := strings.Replace(noBrHtml, "\\n", "", -1)

// remove end-of para markers, not needed for pdf print
noEndParaHtml := strings.Replace(noLfHtml, "</p>", "", -1)

rmap["Item Response"] = noEndParaHtml

select {
case out <- rmap:
Expand Down
16 changes: 13 additions & 3 deletions nap-writing-print/script-writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ func createScriptWriter(ctx context.Context, in <-chan map[string]string) (<-cha
doc.SetColor("black").
SetFont("Helvetica-Bold", 24).
SetXY(3.5, 2.7).DrawText(rmap["Anonymised Id"])
//
// draw the column of text
var colText = strings.Replace(rmap["Item Response"], "\\n", " ", -1)

colText := splitParas(rmap["Item Response"])
doc.SetColor("black").
SetFont("Helvetica", 12).
// DrawUnitGrid().
DrawTextInBox(3.5, 4, 12, 28, "LT", colText)

//
// save the files
doc.SaveFile(outputFile1)
Expand All @@ -57,3 +57,13 @@ func createScriptWriter(ctx context.Context, in <-chan map[string]string) (<-cha
return errc, nil

}

//
// use the paragraph markers that have been left in the text
// to insert spacing characters.
//
func splitParas(fullText string) string {

return strings.Replace(fullText, "<p>", "\n\n", -1)

}

0 comments on commit c9c929e

Please sign in to comment.