summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoop Kiefte <ikojba@gmail.com>2020-11-26 14:53:47 +0200
committerJoop Kiefte <ikojba@gmail.com>2020-11-26 14:53:47 +0200
commit01fac81382cb49448d767d4d0d74aece4a4ec454 (patch)
tree5d5c7e236ee11a66a9b61730a9372363c810794d
parentef6d0f0d41abc5096088ae60c2ca7732cb794960 (diff)
Make much faster with profiling, fixing newline bug
-rw-r--r--main.go11
-rw-r--r--pdf/create.go32
2 files changed, 26 insertions, 17 deletions
diff --git a/main.go b/main.go
index 0cc46f9..009cb76 100644
--- a/main.go
+++ b/main.go
@@ -16,11 +16,12 @@ import (
"os"
"strings"
"time"
+
)
func main() {
start := time.Now()
- defer func () {
+ defer func() {
log.Printf("Conversion took %v", time.Since(start))
}()
config := flag.String("config", "lexington.toml", "Configuration file to use.")
@@ -50,18 +51,18 @@ func main() {
}
ins := strings.Split(*input, ".")
- if len(ins)>1 && *from == "" {
+ if len(ins) > 1 && *from == "" {
*from = ins[len(ins)-1]
}
- if len(ins)>2 && *scenein == "" {
+ if len(ins) > 2 && *scenein == "" {
*scenein = ins[len(ins)-2]
}
outs := strings.Split(*output, ".")
- if len(outs)>1 && *to == "" {
+ if len(outs) > 1 && *to == "" {
*to = outs[len(outs)-1]
}
- if len(outs)>2 && *sceneout == "" {
+ if len(outs) > 2 && *sceneout == "" {
*sceneout = outs[len(outs)-2]
}
diff --git a/pdf/create.go b/pdf/create.go
index c4450a7..3a65984 100644
--- a/pdf/create.go
+++ b/pdf/create.go
@@ -16,10 +16,11 @@ type Tree struct {
PDF *gofpdf.Fpdf
Rules rules.Set
F lex.Screenplay
+ HTML gofpdf.HTMLBasicType
}
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)
+ line(t.PDF, t.Rules.Get(a), t.HTML, t.Rules.Get(a).Prefix+text+t.Rules.Get(a).Postfix)
}
func (t Tree) Render() {
@@ -78,23 +79,29 @@ var (
underline = regexp.MustCompile("_{1}([^\\*\n]+)_{1}")
)
-func line(pdf *gofpdf.Fpdf, format rules.Format, text string) {
- if format.Align == "C" {
- text = "<center>"+text+"</center>"
- }
+func line(pdf *gofpdf.Fpdf, format rules.Format, html gofpdf.HTMLBasicType, text string) {
pdf.SetFont(format.Font, format.Style, format.Size)
pdf.SetX(0)
pdf.SetLeftMargin(format.Left)
pdf.SetRightMargin(format.Right)
- text = bolditalic.ReplaceAllString(text, "<b><i>$1</i></b>")
- text = bold.ReplaceAllString(text, "<b>$1</b>")
- text = italic.ReplaceAllString(text, "<i>$1</i>")
- text = underline.ReplaceAllString(text, "<u>$1</u>")
+ text = strings.TrimRight(text, "\r\n")
+
+ if strings.ContainsAny(text, "*_") {
+ text = bolditalic.ReplaceAllString(text, "<b><i>$1</i></b>")
+ text = bold.ReplaceAllString(text, "<b>$1</b>")
+ text = italic.ReplaceAllString(text, "<i>$1</i>")
+ text = underline.ReplaceAllString(text, "<u>$1</u>")
+
+ if format.Align == "C" {
+ text = "<center>"+text+"</center>"
+ }
+ html.Write(0.165, text)
+ pdf.SetY(pdf.GetY()+0.165)
+ return
+ }
- html := pdf.HTMLBasicNew()
- html.Write(0.165, text)
- pdf.SetY(pdf.GetY()+0.165)
+ pdf.MultiCell(0, 0.165, text, "", format.Align, false)
}
func Create(file string, format rules.Set, contents lex.Screenplay) {
@@ -110,6 +117,7 @@ func Create(file string, format rules.Set, contents lex.Screenplay) {
PDF: pdf,
Rules: format,
F: contents,
+ HTML: pdf.HTMLBasicNew(),
}
f.Render()
err := pdf.OutputFileAndClose(file)