Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to gomarkdown #40

Merged
merged 7 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ The tests create log files that trace the BlackFriday parser
callbacks. This is a valuable debug tool showing each callback
and data provided in each while the AST is presented.

2019-09-23: It appears that the BlackFriday project is no longer active.
There is a fork `gomarkdown/markdown` that I may be able to move to
in the future if needed.

## Supported Markdown
The supported elements of markdown are:
- Emphasized and strong text
Expand Down
2 changes: 1 addition & 1 deletion cmd/md2pdf/md2pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func main() {
backgroundColor := "white"
if *themeArg == "dark" {
theme = mdtopdf.DARK
backgroundColor = "eerieblack"
backgroundColor = "black"
textColor = mdtopdf.Colorlookup("darkgray")
fillColor = mdtopdf.Colorlookup("black")
}
Expand Down
Binary file modified cmd/md2pdf/russian.pdf
Binary file not shown.
6 changes: 3 additions & 3 deletions cmd/md2pdf/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ The below is a blockquote with inner codeblocks.
> return "working";
> }

Here is the fpdf logo (inline): ![from https://github.com/go-pdf/fpdf/tree/master/image](../image/fpdf.png)
Here is the fpdf logo (inline): ![from https://github.com/go-pdf/fpdf/tree/master/image](../../image/fpdf.png)

Here is a Gopher:

![from https://github.com/egonelbre/gophers](../image/hiking.png "Optional title")
![from https://github.com/egonelbre/gophers](../../image/hiking.png "Optional title")

*The Go gopher was designed by Renee French. The Gopher character design is licensed under the Creative Commons 3.0 Attributions license. Read http://blog.golang.org/gopher for more details.*


__This is the last line of the document.__
__This is the last line of the document.__
Binary file modified cmd/md2pdf/test.pdf
Binary file not shown.
5 changes: 0 additions & 5 deletions containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@

package mdtopdf

import (
bf "github.com/russross/blackfriday/v2"
)

type listType int

const (
Expand Down Expand Up @@ -54,7 +50,6 @@ func (n listType) String() string {
}

type containerState struct {
containerType bf.NodeType
textStyle Styler
leftMargin float64
firstParagraph bool
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/canhlinh/svg2png v0.0.0-20201124065332-6ba87c82371f
github.com/gabriel-vasile/mimetype v1.4.2
github.com/go-pdf/fpdf v0.8.0
github.com/gomarkdown/markdown v0.0.0-20230716120725-531d2d74bc12 // indirect
github.com/jessp01/gohighlight v0.21.1-7
github.com/mcuadros/go-version v0.0.0-20190830083331-035f6764e8d2 // indirect
github.com/russross/blackfriday/v2 v2.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/go-pdf/fpdf v0.8.0 h1:IJKpdaagnWUeSkUFUjTcSzTppFxmv8ucGQyNPQWxYOQ=
github.com/go-pdf/fpdf v0.8.0/go.mod h1:gfqhcNwXrsd3XYKte9a7vM3smvU/jB4ZRDrmWSxpfdc=
github.com/gomarkdown/markdown v0.0.0-20230716120725-531d2d74bc12 h1:uK3X/2mt4tbSGoHvbLBHUny7CKiuwUip3MArtukol4E=
github.com/gomarkdown/markdown v0.0.0-20230716120725-531d2d74bc12/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
github.com/jessp01/gohighlight v0.21.1-7 h1:u1CurHm8ogal3AFeDMF97pDJ4aPlPdgZg5PL6ULeTb0=
github.com/jessp01/gohighlight v0.21.1-7/go.mod h1:52r0Yxd1+T9f7uLenaO2/34K3gPOejxCxXwdNc/2Z8Y=
github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
Expand Down
2 changes: 1 addition & 1 deletion highlight
101 changes: 53 additions & 48 deletions mdtopdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ import (
"fmt"
"io"
"os"
"reflect"
"strings"

"github.com/go-pdf/fpdf"
bf "github.com/russross/blackfriday/v2"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/ast"
"github.com/gomarkdown/markdown/parser"
)

// Color is a RGB set of ints; for a nice picker
Expand Down Expand Up @@ -119,6 +122,7 @@ type PdfRenderer struct {
InputBaseURL string
Theme Theme
BackgroundColor Color
documentMatter ast.DocumentMatters // keep track of front/main/back matter.
}

// SetLightTheme sets theme to 'light'
Expand Down Expand Up @@ -170,11 +174,11 @@ func (r *PdfRenderer) SetLightTheme() {

// SetDarkTheme sets theme to 'dark'
func (r *PdfRenderer) SetDarkTheme() {
r.BackgroundColor = Colorlookup("eerieblack")
r.BackgroundColor = Colorlookup("black")
r.SetPageBackground("", r.BackgroundColor)
// Normal Text
r.Normal = Styler{Font: "Arial", Style: "", Size: 12, Spacing: 2,
FillColor: Colorlookup("black"), TextColor: Colorlookup("darkgray")}
FillColor: Colorlookup("black"), TextColor: Colorlookup("white")}

// Link text
r.Link = Styler{Font: "Arial", Style: "b", Size: 12, Spacing: 2,
Expand Down Expand Up @@ -264,7 +268,7 @@ func NewPdfRenderer(orient, papersz, pdfFile, tracerFile string, opts []RenderOp
r.IndentValue = 3 * r.em

r.cs = states{stack: make([]*containerState, 0)}
initcurrent := &containerState{containerType: bf.Paragraph,
initcurrent := &containerState{
listkind: notlist,
textStyle: r.Normal, leftMargin: r.mleft}
r.cs.push(initcurrent)
Expand Down Expand Up @@ -318,22 +322,24 @@ func (r *PdfRenderer) Process(content []byte) error {
// Run takes the markdown content, parses it but don't generate the PDF. you can access the PDF with youRenderer.Pdf
func (r *PdfRenderer) Run(content []byte) error {
// Preprocess content by changing all CRLF to LF
s := string(content)
s = strings.ReplaceAll(s, "\r\n", "\n")
s := content
s = markdown.NormalizeNewlines(s)

if r.unicodeTranslator != nil {
s = r.unicodeTranslator(s)
s = []byte(r.unicodeTranslator(string(s)))
}

content = []byte(s)
_ = bf.Run(content, bf.WithRenderer(r))
exts := parser.CommonExtensions // parser.OrderedListStart | parser.NoEmptyLineBeforeBlock
p := parser.NewWithExtensions(exts)
doc := markdown.Parse(s, p)
_ = markdown.Render(doc, r)

return nil
}

// UpdateParagraphStyler - update with default styler
func (r *PdfRenderer) UpdateParagraphStyler(defaultStyler Styler) {
initcurrent := &containerState{containerType: bf.Paragraph,
initcurrent := &containerState{
listkind: notlist,
textStyle: defaultStyler, leftMargin: r.mleft}
r.cs.push(initcurrent)
Expand Down Expand Up @@ -379,76 +385,75 @@ func (r *PdfRenderer) writeLink(s Styler, display, url string) {
// The typical behavior is to return GoToNext, which asks for the usual
// traversal to the next node.
// (above taken verbatim from the blackfriday v2 package)
func (r *PdfRenderer) RenderNode(w io.Writer, node *bf.Node, entering bool) bf.WalkStatus {
switch node.Type {
case bf.Text:
func (r *PdfRenderer) RenderNode(w io.Writer, node ast.Node, entering bool) ast.WalkStatus {
switch node := node.(type) {
case *ast.Text:
r.processText(node)
case bf.Softbreak:
case *ast.Softbreak:
r.tracer("Softbreak", "Output newline")
r.cr()
case bf.Hardbreak:
case *ast.Hardbreak:
r.tracer("Hardbreak", "Output newline")
r.cr()
case bf.Emph:
case *ast.Emph:
r.processEmph(node, entering)
case bf.Strong:
case *ast.Strong:
r.processStrong(node, entering)
case bf.Del:
case *ast.Del:
if entering {
r.tracer("DEL (entering)", "Not handled")
} else {
r.tracer("DEL (leaving)", "Not handled")
}
case bf.HTMLSpan:
case *ast.HTMLSpan:
r.tracer("HTMLSpan", "Not handled")
case bf.Link:
r.processLink(node, entering)
case bf.Image:
r.processImage(node, entering)
case bf.Code:
case *ast.Link:
r.processLink(*node, entering)
case *ast.Image:
r.processImage(*node, entering)
case *ast.Code:
r.processCode(node)
case bf.Document:
case *ast.Document:
r.tracer("Document", "Not Handled")
case bf.Paragraph:
case *ast.Paragraph:
r.processParagraph(node, entering)
case bf.BlockQuote:
case *ast.BlockQuote:
r.processBlockQuote(node, entering)
case bf.HTMLBlock:
case *ast.HTMLBlock:
r.processHTMLBlock(node)
case bf.Heading:
r.processHeading(node, entering)
case bf.HorizontalRule:
case *ast.Heading:
r.processHeading(*node, entering)
case *ast.HorizontalRule:
r.processHorizontalRule(node)
case bf.List:
r.processList(node, entering)
case bf.Item:
r.processItem(node, entering)
case bf.CodeBlock:
r.processCodeblock(node)
case bf.Table:
case *ast.List:
r.processList(*node, entering)
case *ast.ListItem:
r.processItem(*node, entering)
case *ast.CodeBlock:
r.processCodeblock(*node)
case *ast.Table:
r.processTable(node, entering)
case bf.TableHead:
case *ast.TableHeader:
r.processTableHead(node, entering)
case bf.TableBody:
case *ast.TableBody:
r.processTableBody(node, entering)
case bf.TableRow:
case *ast.TableRow:
r.processTableRow(node, entering)
case bf.TableCell:
r.processTableCell(node, entering)
case *ast.TableCell:
r.processTableCell(*node, entering)
default:
panic("Unknown node type " + node.Type.String())
panic("Unknown node type " + reflect.TypeOf(node).Name())
}
return bf.GoToNext
return ast.GoToNext
}

// RenderHeader is not supported.
func (r *PdfRenderer) RenderHeader(w io.Writer, ast *bf.Node) {
func (r *PdfRenderer) RenderHeader(w io.Writer, ast ast.Node) {
r.tracer("RenderHeader", "Not handled")
}

// RenderFooter is not supported.
func (r *PdfRenderer) RenderFooter(w io.Writer, ast *bf.Node) {
r.tracer("RenderFooter", "Not handled")
func (r *PdfRenderer) RenderFooter(w io.Writer, _ ast.Node) {
}

func (r *PdfRenderer) cr() {
Expand Down
Loading