Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
canifa_note
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Hoàng Anh
canifa_note
Commits
5266a626
Commit
5266a626
authored
Dec 13, 2023
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: implement html renderer
parent
43ef9eac
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
103 additions
and
12 deletions
+103
-12
paragraph.go
plugin/gomark/parser/paragraph.go
+5
-4
paragraph_test.go
plugin/gomark/parser/paragraph_test.go
+26
-0
parser_test.go
plugin/gomark/parser/parser_test.go
+23
-2
html.go
plugin/gomark/renderer/html/html.go
+40
-3
html_test.go
plugin/gomark/renderer/html/html_test.go
+9
-3
No files found.
plugin/gomark/parser/paragraph.go
View file @
5266a626
...
@@ -17,17 +17,18 @@ func NewParagraphParser() *ParagraphParser {
...
@@ -17,17 +17,18 @@ func NewParagraphParser() *ParagraphParser {
func
(
*
ParagraphParser
)
Match
(
tokens
[]
*
tokenizer
.
Token
)
(
int
,
bool
)
{
func
(
*
ParagraphParser
)
Match
(
tokens
[]
*
tokenizer
.
Token
)
(
int
,
bool
)
{
contentTokens
:=
[]
*
tokenizer
.
Token
{}
contentTokens
:=
[]
*
tokenizer
.
Token
{}
cursor
:=
0
for
_
,
token
:=
range
tokens
{
for
;
cursor
<
len
(
tokens
);
cursor
++
{
contentTokens
=
append
(
contentTokens
,
token
)
token
:=
tokens
[
cursor
]
if
token
.
Type
==
tokenizer
.
Newline
{
if
token
.
Type
==
tokenizer
.
Newline
{
break
break
}
}
contentTokens
=
append
(
contentTokens
,
token
)
}
}
if
len
(
contentTokens
)
==
0
{
if
len
(
contentTokens
)
==
0
{
return
0
,
false
return
0
,
false
}
}
if
len
(
contentTokens
)
==
1
&&
contentTokens
[
0
]
.
Type
==
tokenizer
.
Newline
{
return
0
,
false
}
return
len
(
contentTokens
),
true
return
len
(
contentTokens
),
true
}
}
...
...
plugin/gomark/parser/paragraph_test.go
View file @
5266a626
...
@@ -18,6 +18,10 @@ func TestParagraphParser(t *testing.T) {
...
@@ -18,6 +18,10 @@ func TestParagraphParser(t *testing.T) {
text
:
""
,
text
:
""
,
paragraph
:
nil
,
paragraph
:
nil
,
},
},
{
text
:
"
\n
"
,
paragraph
:
nil
,
},
{
{
text
:
"Hello world!"
,
text
:
"Hello world!"
,
paragraph
:
&
ast
.
Paragraph
{
paragraph
:
&
ast
.
Paragraph
{
...
@@ -28,6 +32,28 @@ func TestParagraphParser(t *testing.T) {
...
@@ -28,6 +32,28 @@ func TestParagraphParser(t *testing.T) {
},
},
},
},
},
},
{
text
:
"Hello world!
\n
"
,
paragraph
:
&
ast
.
Paragraph
{
Children
:
[]
ast
.
Node
{
&
ast
.
Text
{
Content
:
"Hello world!"
,
},
&
ast
.
LineBreak
{},
},
},
},
{
text
:
"Hello world!
\n\n
New paragraph."
,
paragraph
:
&
ast
.
Paragraph
{
Children
:
[]
ast
.
Node
{
&
ast
.
Text
{
Content
:
"Hello world!"
,
},
&
ast
.
LineBreak
{},
},
},
},
}
}
for
_
,
test
:=
range
tests
{
for
_
,
test
:=
range
tests
{
...
...
plugin/gomark/parser/parser_test.go
View file @
5266a626
...
@@ -57,9 +57,9 @@ func TestParser(t *testing.T) {
...
@@ -57,9 +57,9 @@ func TestParser(t *testing.T) {
&
ast
.
Text
{
&
ast
.
Text
{
Content
:
"!"
,
Content
:
"!"
,
},
},
&
ast
.
LineBreak
{},
},
},
},
},
&
ast
.
LineBreak
{},
&
ast
.
Paragraph
{
&
ast
.
Paragraph
{
Children
:
[]
ast
.
Node
{
Children
:
[]
ast
.
Node
{
&
ast
.
Text
{
&
ast
.
Text
{
...
@@ -84,15 +84,36 @@ func TestParser(t *testing.T) {
...
@@ -84,15 +84,36 @@ func TestParser(t *testing.T) {
&
ast
.
Text
{
&
ast
.
Text
{
Content
:
"!"
,
Content
:
"!"
,
},
},
&
ast
.
LineBreak
{},
},
},
},
},
&
ast
.
LineBreak
{},
&
ast
.
CodeBlock
{
&
ast
.
CodeBlock
{
Language
:
"javascript"
,
Language
:
"javascript"
,
Content
:
"console.log(
\"
Hello world!
\"
);"
,
Content
:
"console.log(
\"
Hello world!
\"
);"
,
},
},
},
},
},
},
{
text
:
"Hello world!
\n\n
New paragraph."
,
nodes
:
[]
ast
.
Node
{
&
ast
.
Paragraph
{
Children
:
[]
ast
.
Node
{
&
ast
.
Text
{
Content
:
"Hello world!"
,
},
&
ast
.
LineBreak
{},
},
},
&
ast
.
LineBreak
{},
&
ast
.
Paragraph
{
Children
:
[]
ast
.
Node
{
&
ast
.
Text
{
Content
:
"New paragraph."
,
},
},
},
},
},
}
}
for
_
,
test
:=
range
tests
{
for
_
,
test
:=
range
tests
{
...
...
plugin/gomark/renderer/html/html.go
View file @
5266a626
...
@@ -11,17 +11,17 @@ import (
...
@@ -11,17 +11,17 @@ import (
// nolint
// nolint
type
HTMLRenderer
struct
{
type
HTMLRenderer
struct
{
output
*
bytes
.
Buffer
output
*
bytes
.
Buffer
context
*
rend
erContext
context
*
Render
erContext
}
}
type
rend
erContext
struct
{
type
Render
erContext
struct
{
}
}
// NewHTMLRenderer creates a new HTMLRenderer.
// NewHTMLRenderer creates a new HTMLRenderer.
func
NewHTMLRenderer
()
*
HTMLRenderer
{
func
NewHTMLRenderer
()
*
HTMLRenderer
{
return
&
HTMLRenderer
{
return
&
HTMLRenderer
{
output
:
new
(
bytes
.
Buffer
),
output
:
new
(
bytes
.
Buffer
),
context
:
&
rend
erContext
{},
context
:
&
Render
erContext
{},
}
}
}
}
...
@@ -57,6 +57,43 @@ func (r *HTMLRenderer) RenderNode(node ast.Node) {
...
@@ -57,6 +57,43 @@ func (r *HTMLRenderer) RenderNode(node ast.Node) {
if
prevSibling
==
nil
||
prevSibling
.
Type
()
!=
ast
.
NodeTypeBlockquote
{
if
prevSibling
==
nil
||
prevSibling
.
Type
()
!=
ast
.
NodeTypeBlockquote
{
r
.
output
.
WriteString
(
"</blockquote>"
)
r
.
output
.
WriteString
(
"</blockquote>"
)
}
}
case
*
ast
.
BoldItalic
:
r
.
output
.
WriteString
(
"<strong><em>"
)
r
.
output
.
WriteString
(
n
.
Content
)
r
.
output
.
WriteString
(
"</em></strong>"
)
case
*
ast
.
Bold
:
r
.
output
.
WriteString
(
"<strong>"
)
r
.
output
.
WriteString
(
n
.
Content
)
r
.
output
.
WriteString
(
"</strong>"
)
case
*
ast
.
Italic
:
r
.
output
.
WriteString
(
"<em>"
)
r
.
output
.
WriteString
(
n
.
Content
)
r
.
output
.
WriteString
(
"</em>"
)
case
*
ast
.
Code
:
r
.
output
.
WriteString
(
"<code>"
)
r
.
output
.
WriteString
(
n
.
Content
)
r
.
output
.
WriteString
(
"</code>"
)
case
*
ast
.
Link
:
r
.
output
.
WriteString
(
`<a href="`
)
r
.
output
.
WriteString
(
n
.
URL
)
r
.
output
.
WriteString
(
`">`
)
r
.
output
.
WriteString
(
n
.
Text
)
r
.
output
.
WriteString
(
"</a>"
)
case
*
ast
.
Image
:
r
.
output
.
WriteString
(
`<img src="`
)
r
.
output
.
WriteString
(
n
.
URL
)
r
.
output
.
WriteString
(
`" alt="`
)
r
.
output
.
WriteString
(
n
.
AltText
)
r
.
output
.
WriteString
(
`" />`
)
case
*
ast
.
Tag
:
r
.
output
.
WriteString
(
`<span>`
)
r
.
output
.
WriteString
(
`# `
)
r
.
output
.
WriteString
(
n
.
Content
)
r
.
output
.
WriteString
(
`</span>`
)
case
*
ast
.
Strikethrough
:
r
.
output
.
WriteString
(
`<del>`
)
r
.
output
.
WriteString
(
n
.
Content
)
r
.
output
.
WriteString
(
`</del>`
)
case
*
ast
.
Text
:
case
*
ast
.
Text
:
r
.
output
.
WriteString
(
n
.
Content
)
r
.
output
.
WriteString
(
n
.
Content
)
default
:
default
:
...
...
plugin/gomark/renderer/html/html_test.go
View file @
5266a626
...
@@ -22,6 +22,14 @@ func TestHTMLRenderer(t *testing.T) {
...
@@ -22,6 +22,14 @@ func TestHTMLRenderer(t *testing.T) {
text
:
"> Hello
\n
> world!"
,
text
:
"> Hello
\n
> world!"
,
expected
:
`<blockquote>Hello<br>world!</blockquote>`
,
expected
:
`<blockquote>Hello<br>world!</blockquote>`
,
},
},
{
text
:
"*Hello* world!"
,
expected
:
`<p><em>Hello</em> world!</p>`
,
},
{
text
:
"**Hello** world!"
,
expected
:
`<p><strong>Hello</strong> world!</p>`
,
},
}
}
for
_
,
test
:=
range
tests
{
for
_
,
test
:=
range
tests
{
...
@@ -29,8 +37,6 @@ func TestHTMLRenderer(t *testing.T) {
...
@@ -29,8 +37,6 @@ func TestHTMLRenderer(t *testing.T) {
nodes
,
err
:=
parser
.
Parse
(
tokens
)
nodes
,
err
:=
parser
.
Parse
(
tokens
)
require
.
NoError
(
t
,
err
)
require
.
NoError
(
t
,
err
)
actual
:=
NewHTMLRenderer
()
.
Render
(
nodes
)
actual
:=
NewHTMLRenderer
()
.
Render
(
nodes
)
if
actual
!=
test
.
expected
{
require
.
Equal
(
t
,
test
.
expected
,
actual
)
t
.
Errorf
(
"expected: %s, actual: %s"
,
test
.
expected
,
actual
)
}
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment