From c9c929e2d6ca39a61fa1dc6f636fe8eb884bb488 Mon Sep 17 00:00:00 2001 From: matt-farmer Date: Mon, 16 Apr 2018 14:21:23 +1000 Subject: [PATCH] Maintain paragraphing, remove escaped chars Maintains the original paragraph strucures Handles escaped chars e.g emojis --- ...sample.csv => writing-response-sample.csv} | 0 nap-writing-print/html-sanitizer.go | 19 +++++++++++++++++-- nap-writing-print/script-writer.go | 16 +++++++++++++--- 3 files changed, 30 insertions(+), 5 deletions(-) rename app/nap-writing-print/in/{sample.csv => writing-response-sample.csv} (100%) diff --git a/app/nap-writing-print/in/sample.csv b/app/nap-writing-print/in/writing-response-sample.csv similarity index 100% rename from app/nap-writing-print/in/sample.csv rename to app/nap-writing-print/in/writing-response-sample.csv diff --git a/nap-writing-print/html-sanitizer.go b/nap-writing-print/html-sanitizer.go index 7934865..f7c3e79 100644 --- a/nap-writing-print/html-sanitizer.go +++ b/nap-writing-print/html-sanitizer.go @@ -4,6 +4,8 @@ package nap_writing_print import ( "context" + "html" + "strings" "github.com/microcosm-cc/bluemonday" ) @@ -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, " ", " ", -1) + noLfHtml := strings.Replace(noBrHtml, "\\n", "", -1) + + // remove end-of para markers, not needed for pdf print + noEndParaHtml := strings.Replace(noLfHtml, "

", "", -1) + + rmap["Item Response"] = noEndParaHtml select { case out <- rmap: diff --git a/nap-writing-print/script-writer.go b/nap-writing-print/script-writer.go index 2cd6ba2..3b84893 100644 --- a/nap-writing-print/script-writer.go +++ b/nap-writing-print/script-writer.go @@ -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) @@ -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, "

", "\n\n", -1) + +}