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

Markdown #24

Merged
merged 53 commits into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
4a26e3f
add markdown develop
tiechui1994 Jul 13, 2020
a6edcfe
text convert to pdf
tiechui1994 Jul 13, 2020
d571d34
update
tiechui1994 Jul 14, 2020
e78ca4d
build new component
tiechui1994 Jul 15, 2020
35e6e6a
update
tiechui1994 Jul 15, 2020
852e8fc
add header componet
tiechui1994 Jul 16, 2020
37b6d6d
rename file name
tiechui1994 Jul 16, 2020
f7572bb
use re
tiechui1994 Jul 16, 2020
c4831d8
update
tiechui1994 Jul 17, 2020
388bd3c
add regex for md elements
tiechui1994 Jul 17, 2020
49df65e
add new method to regex block
tiechui1994 Jul 18, 2020
36fa19f
use js's markdown library to parse markdown text
tiechui1994 Jul 19, 2020
5db4ef2
update
tiechui1994 Jul 20, 2020
9a7a629
add lex params
tiechui1994 Jul 20, 2020
63d2c5b
update
tiechui1994 Jul 21, 2020
393be1b
update
tiechui1994 Jul 22, 2020
b9c43ae
update
tiechui1994 Jul 23, 2020
95f1477
add regex2 support
tiechui1994 Jul 25, 2020
131e835
update
tiechui1994 Jul 25, 2020
77a0268
add lex
tiechui1994 Jul 25, 2020
e8c42e1
add new test for lex
tiechui1994 Aug 5, 2020
6a698ac
add test for basic markdown
tiechui1994 Aug 7, 2020
de332de
update
tiechui1994 Aug 7, 2020
3266cb3
add list
tiechui1994 Aug 7, 2020
137322b
add more picture type support
tiechui1994 Aug 7, 2020
532f13d
add new picture type
tiechui1994 Aug 8, 2020
26ad5a8
update text component
tiechui1994 Aug 8, 2020
c4b506a
update js script
tiechui1994 Aug 8, 2020
b361ad6
add new content:
tiechui1994 Aug 8, 2020
8a66387
remove unued js file and update markdown
tiechui1994 Aug 9, 2020
d510266
add default picture for image not exist
tiechui1994 Aug 10, 2020
259ba09
add new picture line
tiechui1994 Aug 11, 2020
149aee8
update image auto break
tiechui1994 Aug 11, 2020
54c78dd
update util method
tiechui1994 Aug 11, 2020
eac9cc3
remove redundant code
tiechui1994 Aug 12, 2020
7bbbcd0
add support netest blockquote
tiechui1994 Aug 12, 2020
d796bd2
update
tiechui1994 Aug 12, 2020
3fa57bd
remove js redument code
tiechui1994 Aug 12, 2020
cc44e59
update
tiechui1994 Aug 12, 2020
b1dae13
add raw style validation
tiechui1994 Aug 13, 2020
7c15a43
update
tiechui1994 Aug 13, 2020
93ebb62
update
tiechui1994 Aug 14, 2020
c4264f1
rename lex to token and add some convert function
tiechui1994 Aug 15, 2020
83b5cd4
add lex for convert function
tiechui1994 Aug 15, 2020
2a5cfe5
rebuild regex2
tiechui1994 Aug 15, 2020
9485e02
add regex for 'g' params
tiechui1994 Aug 16, 2020
0ab3f1f
update
tiechui1994 Aug 16, 2020
849b6de
add test fuction
tiechui1994 Aug 16, 2020
3599ecf
add log for js and success to export data from golang js
tiechui1994 Aug 16, 2020
e0b38f4
can convert basic markdown doc
tiechui1994 Aug 17, 2020
6c48f87
redefine token
tiechui1994 Aug 17, 2020
c8cb4a8
update
tiechui1994 Aug 17, 2020
deed1c2
rename dir name
tiechui1994 Aug 18, 2020
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
Prev Previous commit
Next Next commit
update util method
  • Loading branch information
tiechui1994 committed Aug 18, 2020
commit 54c78ddd7f06428ef544dcb97e1a6c38622aaf01
2 changes: 1 addition & 1 deletion cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (cell *TextCell) GenerateAtomicCell(maxheight float64) (int, int, error) {

// 字体颜色控制
if !util.IsEmpty(cell.fontColor) {
cell.pdf.TextColor(util.GetColorRGB(cell.fontColor))
cell.pdf.TextColor(util.RGB(cell.fontColor))
}

cell.pdf.Cell(x, y, cell.contents[i])
Expand Down
4 changes: 2 additions & 2 deletions core/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,10 +488,10 @@ func (report *Report) BackgroundColor(x, y, w, h float64, bgcolor string, lines
lines += "0"
}

bgred, bggreen, bgblue := util.GetColorRGB(bgcolor)
bgred, bggreen, bgblue := util.RGB(bgcolor)
var lred, lgreen, lblue = 1, 1, 1
if lcolor != nil {
lred, lgreen, lblue = util.GetColorRGB(lcolor[0])
lred, lgreen, lblue = util.RGB(lcolor[0])
}

report.addAtomicCell("BC|" + util.Ftoa(x) + "|" + util.Ftoa(y) + "|" + util.Ftoa(w) + "|" +
Expand Down
2 changes: 1 addition & 1 deletion div.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func (div *Div) GenerateAtomicCell() error {
}

if !util.IsEmpty(div.fontColor) {
div.pdf.TextColor(util.GetColorRGB(div.fontColor))
div.pdf.TextColor(util.RGB(div.fontColor))
}
div.pdf.Font(div.font.Family, div.font.Size, div.font.Style) // 添加设置
div.pdf.Cell(x, y, div.contents[i])
Expand Down
153 changes: 83 additions & 70 deletions markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
"bytes"
"regexp"
"encoding/json"

"github.com/tiechui1994/gopdf/core"
"net/http"
"os"
"time"
"io"

"github.com/tiechui1994/gopdf/core"
"github.com/tiechui1994/gopdf/util"
)

const (
Expand Down Expand Up @@ -46,6 +47,21 @@ const (
defaultCodeSpanFontSize = 10.0
)

const (
spaceLen = 4.425
)

const (
color_black = "1,1,1"
color_gray = "128,128,128"
color_white = "255,255,255"

color_pink = "199,37,78"
color_lightgray = "220,220,220"
color_whitesmoke = "245,245,245"
color_blue = "0,0,255"
)

// Token is parse markdown result element
type Token struct {
Type string `json:"type"`
Expand Down Expand Up @@ -96,13 +112,13 @@ type MdText struct {
padding float64 // padding left length
lineHeight float64 // line height

stoped bool // symbol stoped
precision float64 // sigle text char length
length float64
text string // text content
remain string // renain texy
link string // special TYPE_LINK
newlines int
blockquote bool // the cuurent ele is blockquote
stoped bool // symbol stoped
precision float64 // sigle text char length
text string // text content
remain string // renain texy
link string // special TYPE_LINK
newlines int

offsetx float64
offsety float64
Expand Down Expand Up @@ -146,11 +162,11 @@ func (c *MdText) SetText(font interface{}, text ...string) {
subs := re.FindAllString(c.text, -1)
if len(subs) > 0 {
str := re.ReplaceAllString(c.text, "")
c.length = c.pdf.MeasureTextWidth(str)
c.precision = c.length / float64(len([]rune(str)))
length := c.pdf.MeasureTextWidth(str)
c.precision = length / float64(len([]rune(str)))
} else {
c.length = c.pdf.MeasureTextWidth(c.text)
c.precision = c.length / float64(len([]rune(c.text)))
length := c.pdf.MeasureTextWidth(c.text)
c.precision = length / float64(len([]rune(c.text)))
}
}

Expand All @@ -166,34 +182,28 @@ func (c *MdText) GenerateAtomicCell() (pagebreak, over bool, err error) {
for !c.stoped {
switch c.Type {
case TYPE_CODESPAN:
// bg: 248,248,255 text:1,1,1 line: 245,245,245, Typora
// bg: 249,242,244 text:199,37,78 line: 245,245,245 GitLab
c.pdf.BackgroundColor(x1, y, width, 15.0, "249,242,244",
"1111", "245,245,245")

c.pdf.TextColor(199, 37, 78)
c.pdf.BackgroundColor(x1, y, width, 15.0, color_lightgray, "1111", color_whitesmoke)
//
c.pdf.TextColor(util.RGB(color_pink))
c.pdf.Cell(x1, y+3.15, text)
c.pdf.TextColor(1, 1, 1)
c.pdf.TextColor(util.RGB(color_black))
case TYPE_CODE:
// bg: 248,248,255, text: 1,1,1 Typora
// bg: 40,42,54, text: 248,248,242 CSDN
bg := "128,255,128"
c.pdf.BackgroundColor(x1, y, x2-x1, 18.0, bg, "0000")
c.pdf.TextColor(248, 248, 242)
c.pdf.BackgroundColor(x1, y, x2-x1, 18.0, color_whitesmoke, "0000")
c.pdf.TextColor(util.RGB(color_black))
c.pdf.Cell(x1, y+3.15, text)
c.pdf.TextColor(1, 1, 1)
c.pdf.TextColor(util.RGB(color_black))

case TYPE_LINK:
// text, blue
c.pdf.TextColor(0, 0, 255)
c.pdf.TextColor(util.RGB(color_blue))
c.pdf.ExternalLink(x1, y+12.0, 15, text, c.link)
c.pdf.TextColor(1, 1, 1)
c.pdf.TextColor(util.RGB(color_black))

// line
c.pdf.LineColor(0, 0, 255)
c.pdf.LineColor(util.RGB(color_blue))
c.pdf.LineType("solid", 0.4)
c.pdf.LineH(x1, y+c.precision, x1+width)
c.pdf.LineColor(1, 1, 1)
c.pdf.LineColor(util.RGB(color_black))
default:
c.pdf.Cell(x1+c.offsetx, y+c.offsety, text)
}
Expand Down Expand Up @@ -350,9 +360,10 @@ func (c *MdSpace) String() string {

type MdImage struct {
abstractMarkDown
pdf *core.Report
padding float64
Type string
pdf *core.Report
padding float64
blockquote bool
Type string

image *Image
height float64
Expand Down Expand Up @@ -427,11 +438,12 @@ func CommonGenerateAtomicCell(children *[]mardown) (pagebreak, over bool, err er

type MdHeader struct {
abstractMarkDown
pdf *core.Report
fonts map[string]string
children []mardown
padding float64
Type string
pdf *core.Report
fonts map[string]string
children []mardown
padding float64
blockquote bool
Type string
}

func (h *MdHeader) CalFontSizeAndLineHeight(size int) (fontsize int, lineheight float64) {
Expand Down Expand Up @@ -466,11 +478,11 @@ func (h *MdHeader) SetToken(token Token) (err error) {
for _, v := range token.Tokens {
switch v.Type {
case TYPE_TEXT:
text := &MdText{pdf: h.pdf, Type: v.Type, lineHeight: lineheight}
text := &MdText{pdf: h.pdf, Type: v.Type, padding: h.padding, blockquote: h.blockquote, lineHeight: lineheight}
text.SetText(font, v.Text)
h.children = append(h.children, text)
case TYPE_IMAGE:
image := &MdImage{pdf: h.pdf, Type: v.Type, height: lineheight}
image := &MdImage{pdf: h.pdf, Type: v.Type, padding: h.padding, blockquote: h.blockquote, height: lineheight}
h.children = append(h.children, image)
}
}
Expand All @@ -487,11 +499,12 @@ func (h *MdHeader) GenerateAtomicCell() (pagebreak, over bool, err error) {

type MdParagraph struct {
abstractMarkDown
pdf *core.Report
fonts map[string]string
children []mardown
padding float64
Type string
pdf *core.Report
fonts map[string]string
children []mardown
padding float64
blockquote bool
Type string
}

func (p *MdParagraph) SetToken(token Token) error {
Expand Down Expand Up @@ -549,11 +562,12 @@ func (p *MdParagraph) GenerateAtomicCell() (pagebreak, over bool, err error) {

type MdList struct {
abstractMarkDown
pdf *core.Report
fonts map[string]string
children []mardown
padding float64
Type string
pdf *core.Report
fonts map[string]string
children []mardown
padding float64
blockquote bool
Type string
}

func (l *MdList) SetToken(token Token) error {
Expand All @@ -572,7 +586,7 @@ func (l *MdList) SetToken(token Token) error {
space := &MdSpace{pdf: l.pdf, Type: TYPE_SPACE}
l.children = append(l.children, space)

list := &MdList{pdf: l.pdf, fonts: l.fonts, Type: TYPE_LIST, padding: l.padding + 17.7}
list := &MdList{pdf: l.pdf, fonts: l.fonts, Type: TYPE_LIST, padding: l.padding + 4*spaceLen, blockquote: l.blockquote}
list.SetToken(tok)
l.children = append(l.children, list.children...)
continue
Expand All @@ -589,35 +603,35 @@ func (l *MdList) SetToken(token Token) error {
continue

case TYPE_CODE:
code := &MdText{pdf: l.pdf, Type: tok.Type}
code := &MdText{pdf: l.pdf, Type: tok.Type, blockquote: l.blockquote}
code.SetText(l.fonts[FONT_NORMAL], tok.Text)
l.children = append(l.children, code)
continue
}

if token.Ordered {
text := &MdText{pdf: l.pdf, Type: TYPE_TEXT, offsety: -0.45, padding: l.padding}
text := &MdText{pdf: l.pdf, Type: TYPE_TEXT, padding: l.padding, blockquote: l.blockquote, offsety: -0.45}
text.SetText(core.Font{Family: l.fonts[FONT_NORMAL], Size: defaultFontSize}, fmt.Sprintf("%v. ", index+1))
l.children = append(l.children, text)
}

if !token.Ordered {
text := &MdText{pdf: l.pdf, Type: TYPE_TEXT, offsety: -6.3, padding: l.padding}
text := &MdText{pdf: l.pdf, Type: TYPE_TEXT, padding: l.padding, blockquote: l.blockquote, offsety: -6.3}
text.SetText(core.Font{Family: l.fonts[FONT_NORMAL], Size: 28}, "· ")
l.children = append(l.children, text)
}

switch tok.Type {
case TYPE_STRONG:
strong := &MdText{pdf: l.pdf, Type: tok.Type, padding: l.padding}
strong := &MdText{pdf: l.pdf, Type: tok.Type, padding: l.padding, blockquote: l.blockquote}
strong.SetText(l.fonts[FONT_BOLD], tok.Text)
l.children = append(l.children, strong)
case TYPE_LINK:
link := &MdText{pdf: l.pdf, Type: tok.Type, padding: l.padding}
link := &MdText{pdf: l.pdf, Type: tok.Type, padding: l.padding, blockquote: l.blockquote}
link.SetText(l.fonts[FONT_NORMAL], tok.Text, tok.Href)
l.children = append(l.children, link)
case TYPE_TEXT:
text := &MdText{pdf: l.pdf, Type: tok.Type, padding: l.padding}
text := &MdText{pdf: l.pdf, Type: tok.Type, padding: l.padding, blockquote: l.blockquote}
text.SetText(l.fonts[FONT_NORMAL], tok.Text)
l.children = append(l.children, text)
}
Expand Down Expand Up @@ -662,48 +676,47 @@ func (b *MdBlockQuote) SetToken(t Token) error {
return fmt.Errorf("invalid type")
}

b.padding = 17.10

b.padding = 4 * spaceLen
for _, token := range t.Tokens {
switch token.Type {
case TYPE_PARAGRAPH:
paragraph := &MdParagraph{pdf: b.pdf, fonts: b.fonts, Type: token.Type, padding: b.padding}
paragraph := &MdParagraph{pdf: b.pdf, fonts: b.fonts, Type: token.Type, padding: b.padding, blockquote: true}
paragraph.SetToken(token)
b.children = append(b.children, paragraph.children...)
case TYPE_LIST:
list := &MdList{pdf: b.pdf, fonts: b.fonts, Type: token.Type, padding: b.padding}
list := &MdList{pdf: b.pdf, fonts: b.fonts, Type: token.Type, padding: b.padding, blockquote: true}
list.SetToken(token)
b.children = append(b.children, list.children...)
case TYPE_HEADING:
header := &MdHeader{pdf: b.pdf, fonts: b.fonts, Type: token.Type, padding: b.padding}
header := &MdHeader{pdf: b.pdf, fonts: b.fonts, Type: token.Type, padding: b.padding, blockquote: true}
header.SetToken(token)
b.children = append(b.children, header.children...)
case TYPE_SPACE:
space := &MdSpace{pdf: b.pdf, Type: token.Type, padding: b.padding}
b.children = append(b.children, space)
case TYPE_LINK:
link := &MdText{pdf: b.pdf, Type: token.Type, padding: b.padding,}
link.SetText(b.fonts[FONT_NORMAL], token.Text, token.Href, )
link := &MdText{pdf: b.pdf, Type: token.Type, padding: b.padding, blockquote: true}
link.SetText(b.fonts[FONT_NORMAL], token.Text, token.Href)
b.children = append(b.children, link)
case TYPE_TEXT:
text := &MdText{pdf: b.pdf, Type: token.Type, padding: b.padding}
text := &MdText{pdf: b.pdf, Type: token.Type, padding: b.padding, blockquote: true}
text.SetText(b.fonts[FONT_NORMAL], token.Text)
b.children = append(b.children, text)
case TYPE_EM:
log.Println(token.Text)
em := &MdText{pdf: b.pdf, Type: token.Type, padding: b.padding}
em := &MdText{pdf: b.pdf, Type: token.Type, padding: b.padding, blockquote: true}
em.SetText(b.fonts[FONT_IALIC], token.Text)
b.children = append(b.children, em)
case TYPE_CODESPAN:
codespan := &MdText{pdf: b.pdf, Type: token.Type, padding: b.padding}
codespan := &MdText{pdf: b.pdf, Type: token.Type, padding: b.padding, blockquote: true}
codespan.SetText(b.fonts[FONT_NORMAL], token.Text)
b.children = append(b.children, codespan)
case TYPE_CODE:
code := &MdText{pdf: b.pdf, Type: token.Type, padding: b.padding}
code := &MdText{pdf: b.pdf, Type: token.Type, padding: b.padding, blockquote: true}
code.SetText(b.fonts[FONT_NORMAL], token.Text+"\n")
b.children = append(b.children, code)
case TYPE_STRONG:
strong := &MdText{pdf: b.pdf, Type: token.Type, padding: b.padding}
strong := &MdText{pdf: b.pdf, Type: token.Type, padding: b.padding, blockquote: true}
strong.SetText(b.fonts[FONT_BOLD], token.Text)
b.children = append(b.children, strong)
}
Expand Down Expand Up @@ -791,7 +804,6 @@ func NewMarkdownText(pdf *core.Report, x float64, fonts map[string]string) (*Mar
}

func (mt *MarkdownText) SetTokens(tokens []Token) {
log.Println("len", len(tokens))
for _, token := range tokens {
switch token.Type {
case TYPE_PARAGRAPH:
Expand Down Expand Up @@ -842,6 +854,7 @@ func (mt *MarkdownText) SetTokens(tokens []Token) {
}

func (mt *MarkdownText) GenerateAtomicCell() (err error) {
log.Println("children", len(mt.children))
if len(mt.children) == 0 {
return fmt.Errorf("not set text")
}
Expand Down
2 changes: 1 addition & 1 deletion markdown/src/mark.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion markdown/src/mark.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ Images work exactly like links, but they have exclamation points in front. They

![Google Logo](/home/quinn/Pictures/logo_sm.png) and ![Happy].

[Happy]: https://wpclipart.com/smiley/happy/simple_colors/smiley_face_simple_green_small.png ("Smiley face")
[Happy]: /home/quinn/Pictures/logo_sm.png ("Smiley face")


Inline HTML
Expand Down
2 changes: 1 addition & 1 deletion span.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func (span *Span) GenerateAtomicCell() error {
border = span.border

if !util.IsEmpty(span.fontColor) {
span.pdf.TextColor(util.GetColorRGB(span.fontColor))
span.pdf.TextColor(util.RGB(span.fontColor))
}

for i := 0; i < len(span.contents); i++ {
Expand Down
Loading