From d196fe0d713a82945a1427a9f77be39a90087467 Mon Sep 17 00:00:00 2001 From: Joop Kiefte Date: Tue, 24 Nov 2020 20:28:05 +0200 Subject: Add inline markup support --- go.mod | 1 + go.sum | 3 +++ pdf/create.go | 34 +++++++++++++++++++++++++--------- rules/rules.go | 28 ++++++++++++++-------------- 4 files changed, 43 insertions(+), 23 deletions(-) diff --git a/go.mod b/go.mod index 7ed2601..10d03e1 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ require ( github.com/go-bindata/go-bindata v3.1.2+incompatible // indirect github.com/jung-kurt/gofpdf v1.0.0 github.com/phpdave11/gofpdf v1.4.2 + github.com/russross/blackfriday v1.5.2 ) go 1.13 diff --git a/go.sum b/go.sum index 03e7e76..9ee4a80 100644 --- a/go.sum +++ b/go.sum @@ -13,6 +13,9 @@ github.com/phpdave11/gofpdi v1.0.12/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo= +github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= +github.com/russross/blackfriday v1.6.0-pre.1 h1:APCbYYLyOoAWCgZNdHFH3o9uon5Z+N7gp6pThPjuvwI= github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= golang.org/x/image v0.0.0-20190910094157-69e4b8554b2a/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= diff --git a/pdf/create.go b/pdf/create.go index 639b78c..c4450a7 100644 --- a/pdf/create.go +++ b/pdf/create.go @@ -8,6 +8,7 @@ import ( "strconv" "strings" + "regexp" "github.com/phpdave11/gofpdf" ) @@ -21,10 +22,6 @@ func (t Tree) pr(a string, text string) { line(t.PDF, t.Rules.Get(a), t.Rules.Get(a).Prefix+text+t.Rules.Get(a).Postfix) } -func pagenumber() { - -} - func (t Tree) Render() { var block string var lastsection int @@ -34,8 +31,8 @@ func (t Tree) Render() { block = "" t.PDF.AddPage() t.PDF.SetHeaderFuncMode(func() { - t.PDF.SetY(0.5) - t.PDF.SetX(-1) + t.PDF.SetFont("CourierPrime", "", 12) + t.PDF.SetXY(-1, 0.5) t.PDF.Cell(0, 0, strconv.Itoa(t.PDF.PageNo()-1)+".") }, true) continue @@ -74,11 +71,30 @@ func (t Tree) Render() { } } +var ( + bolditalic = regexp.MustCompile("\\*{3}([^\\*\n]+)\\*{3}") + bold = regexp.MustCompile("\\*{2}([^\\*\n]+)\\*{2}") + italic = regexp.MustCompile("\\*{1}([^\\*\n]+)\\*{1}") + underline = regexp.MustCompile("_{1}([^\\*\n]+)_{1}") +) + func line(pdf *gofpdf.Fpdf, format rules.Format, text string) { + if format.Align == "C" { + text = "
"+text+"
" + } pdf.SetFont(format.Font, format.Style, format.Size) - pdf.SetX(format.Left) - pdf.MultiCell(format.Width, 0.165, text, "", format.Align, false) - // TODO: create liner to do away with multicell and add inline markup support + pdf.SetX(0) + pdf.SetLeftMargin(format.Left) + pdf.SetRightMargin(format.Right) + + text = bolditalic.ReplaceAllString(text, "$1") + text = bold.ReplaceAllString(text, "$1") + text = italic.ReplaceAllString(text, "$1") + text = underline.ReplaceAllString(text, "$1") + + html := pdf.HTMLBasicNew() + html.Write(0.165, text) + pdf.SetY(pdf.GetY()+0.165) } func Create(file string, format rules.Set, contents lex.Screenplay) { diff --git a/rules/rules.go b/rules/rules.go index 67db125..ee84dbb 100644 --- a/rules/rules.go +++ b/rules/rules.go @@ -2,7 +2,7 @@ package rules type Format struct { - Width float64 + Right float64 Left float64 Font string Style string @@ -36,59 +36,59 @@ func (s Set) Get(action string) (f Format) { var Default = Set{ "action": { Left: 1.5, - Width: 6, + Right: 1, }, "speaker": { Left: 3.7, - Width: 6.5-3.7, + Right: 1.5, }, "dialog": { Left: 2.5, - Width: 6.5-2.5, + Right: 1.5, }, "scene": { Left: 1.5, - Width: 6, + Right: 1, Style: "b", }, "paren": { Left: 3.1, - Width: 6.5-3.1, + Right: 1.5, }, "trans": { Left: 1.5, - Width: 6, + Right: 1, Align: "R", }, "note": { Left: 1.5, - Width: 6, + Right: 1, }, "allcaps": { Left: 1.5, - Width: 6, + Right: 1, }, "empty": { Left: 1.5, - Width: 6, + Right: 1, }, "title": { Left: 1.5, - Width: 6, + Right: 1, Align: "C", }, "meta": { Left: 1.5, - Width: 6, + Right: 1, }, "center": { Left: 1.5, - Width: 6, + Right: 1, Align: "C", }, "lyrics": { Left: 2, - Width: 5, + Right: 2, Style: "i", Font: "Helvetica", }, -- cgit v1.2.3-70-g09d2