summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-07-17 17:33:48 +0700
committerShulhan <ms@kilabit.info>2022-07-17 17:33:48 +0700
commit5a88e6cc970c315958d044d78e776a3b02bc0c14 (patch)
tree5bff7241f4121095bf004010e183f5ceb3daa038
parent62d968c8aef49e2ce4fd476e5518c1a8fae26e96 (diff)
downloadasciidoctor-go-5a88e6cc970c315958d044d78e776a3b02bc0c14.tar.xz
all: move Parse and parse functions to document
Since the Parse return a *Document it should grouped into document.go to make the code consistent.
-rw-r--r--document.go26
-rw-r--r--document_parser.go27
-rw-r--r--document_parser_test.go232
-rw-r--r--document_test.go235
4 files changed, 261 insertions, 259 deletions
diff --git a/document.go b/document.go
index efb2dad..c4bbd0c 100644
--- a/document.go
+++ b/document.go
@@ -132,6 +132,32 @@ func Open(file string) (doc *Document, err error) {
return doc, nil
}
+// Parse the content into a Document.
+func Parse(content []byte) (doc *Document) {
+ doc = newDocument()
+ parse(doc, content)
+ return doc
+}
+
+func parse(doc *Document, content []byte) {
+ var (
+ docp *documentParser = newDocumentParser(doc, content)
+
+ sectLevel string
+ ok bool
+ )
+
+ docp.parseHeader()
+ docp.doc.postParseHeader()
+
+ sectLevel, ok = doc.Attributes[metaNameSectNumLevel]
+ if ok {
+ doc.sectLevel, _ = strconv.Atoi(sectLevel)
+ }
+
+ docp.parseBlock(doc.preamble, 0)
+}
+
// ToHTMLEmbedded convert the Document object into HTML with content only,
// without header and footer.
func (doc *Document) ToHTMLEmbedded(out io.Writer) (err error) {
diff --git a/document_parser.go b/document_parser.go
index 9992d75..608b95c 100644
--- a/document_parser.go
+++ b/document_parser.go
@@ -6,7 +6,6 @@ package asciidoctor
import (
"bytes"
"fmt"
- "strconv"
"github.com/shuLhan/share/lib/debug"
)
@@ -19,13 +18,6 @@ type documentParser struct {
kind int
}
-// Parse the content into a Document.
-func Parse(content []byte) (doc *Document) {
- doc = newDocument()
- parse(doc, content)
- return doc
-}
-
func newDocumentParser(doc *Document, content []byte) (docp *documentParser) {
docp = &documentParser{
doc: doc,
@@ -37,25 +29,6 @@ func newDocumentParser(doc *Document, content []byte) (docp *documentParser) {
return docp
}
-func parse(doc *Document, content []byte) {
- var (
- docp *documentParser = newDocumentParser(doc, content)
-
- sectLevel string
- ok bool
- )
-
- docp.parseHeader()
- docp.doc.postParseHeader()
-
- sectLevel, ok = doc.Attributes[metaNameSectNumLevel]
- if ok {
- doc.sectLevel, _ = strconv.Atoi(sectLevel)
- }
-
- docp.parseBlock(doc.preamble, 0)
-}
-
func parseSub(parentDoc *Document, content []byte) (subdoc *Document) {
var (
docp *documentParser
diff --git a/document_parser_test.go b/document_parser_test.go
index b5ed4d1..0f3d718 100644
--- a/document_parser_test.go
+++ b/document_parser_test.go
@@ -10,238 +10,6 @@ import (
"github.com/shuLhan/share/lib/test"
)
-func TestParse_metaDocTitle(t *testing.T) {
- type testCase struct {
- content string
- }
-
- var (
- expHTML string = `
-<div class="paragraph">
-<p>Abc begins on a bleary Monday morning.</p>
-</div>`
- )
-
- var cases = []testCase{{
- content: `= Abc
-
-{doctitle} begins on a bleary Monday morning.`,
- }, {
- content: `:doctitle: Abc
-
-{doctitle} begins on a bleary Monday morning.`,
- }}
-
- var (
- doc *Document
- c testCase
- buf bytes.Buffer
- err error
- )
-
- for _, c = range cases {
- doc = Parse([]byte(c.content))
- buf.Reset()
- err = doc.ToHTMLEmbedded(&buf)
- if err != nil {
- t.Fatal(err)
- }
- test.Assert(t, "", expHTML, buf.String())
- }
-}
-
-func TestParse_metaShowTitle(t *testing.T) {
- type testCase struct {
- desc string
- content string
- expHTML string
- }
-
- var cases = []testCase{{
- desc: "default",
- content: `= Abc`,
- expHTML: `
-<div id="header">
-<h1>Abc</h1>
-<div class="details">
-</div>
-</div>
-<div id="content">
-<div id="preamble">
-<div class="sectionbody">
-</div>
-</div>
-</div>
-<div id="footer">
-<div id="footer-text">
-</div>
-</div>`,
- }, {
- desc: "with showtitle!",
- content: `= Abc
-:showtitle!:`,
- expHTML: `
-<div id="header">
-<div class="details">
-</div>
-</div>
-<div id="content">
-<div id="preamble">
-<div class="sectionbody">
-</div>
-</div>
-</div>
-<div id="footer">
-<div id="footer-text">
-</div>
-</div>`,
- }}
-
- var (
- doc *Document
- buf bytes.Buffer
- c testCase
- err error
- )
-
- for _, c = range cases {
- doc = Parse([]byte(c.content))
- buf.Reset()
- err = doc.ToHTMLBody(&buf)
- if err != nil {
- t.Fatal(err)
- }
- test.Assert(t, c.desc, c.expHTML, buf.String())
- }
-}
-
-func TestParse_document_title(t *testing.T) {
- type testCase struct {
- content string
- expString string
- exp DocumentTitle
- }
-
- var cases = []testCase{{
- content: `= Main: sub`,
- exp: DocumentTitle{
- Main: "Main",
- Sub: "sub",
- sep: defTitleSeparator,
- },
- expString: "Main: sub",
- }, {
- // Without space after separator
- content: `= Main:sub`,
- exp: DocumentTitle{
- Main: "Main:sub",
- sep: defTitleSeparator,
- },
- expString: "Main:sub",
- }, {
- // With multiple separator after separator
- content: `= a: b: c`,
- exp: DocumentTitle{
- Main: "a: b",
- Sub: "c",
- sep: defTitleSeparator,
- },
- expString: "a: b: c",
- }, {
- // With custom separator.
- content: `:title-separator: x
-= Mainx sub`,
- exp: DocumentTitle{
- Main: "Main",
- Sub: "sub",
- sep: 'x',
- },
- expString: "Mainx sub",
- }}
-
- var (
- c testCase
- got *Document
- )
-
- for _, c = range cases {
- got = Parse([]byte(c.content))
- test.Assert(t, "Main", c.exp.Main, got.Title.Main)
- test.Assert(t, "Sub", c.exp.Sub, got.Title.Sub)
- test.Assert(t, "sep", c.exp.sep, got.Title.sep)
- test.Assert(t, "String", c.expString, got.Title.String())
- }
-}
-
-func TestParse_author(t *testing.T) {
- type testCase struct {
- desc string
- content string
- exp []*Author
- }
-
- var cases = []testCase{{
- desc: "single author",
- content: `= T
-A B`,
- exp: []*Author{{
- FirstName: "A",
- LastName: "B",
- Initials: "AB",
- }},
- }, {
- desc: "single author with email",
- content: `= T
-A B <a@b>`,
- exp: []*Author{{
- FirstName: "A",
- LastName: "B",
- Initials: "AB",
- Email: "a@b",
- }},
- }, {
- desc: "multiple authors",
- content: `= T
-A B <a@b>; C <c@c>; D e_f G <>;`,
- exp: []*Author{{
- FirstName: "A",
- LastName: "B",
- Initials: "AB",
- Email: "a@b",
- }, {
- FirstName: "C",
- Initials: "C",
- Email: "c@c",
- }, {
- FirstName: "D",
- MiddleName: "e f",
- LastName: "G",
- Initials: "DeG",
- }},
- }, {
- desc: "meta author",
- content: `= T
-:author: A B
-:email: a@b`,
- exp: []*Author{{
- FirstName: "A",
- LastName: "B",
- Initials: "AB",
- Email: "a@b",
- }},
- }}
-
- var (
- c testCase
- got *Document
- )
-
- for _, c = range cases {
- got = Parse([]byte(c.content))
- test.Assert(t, c.desc, c.exp, got.Authors)
- }
-}
-
func TestDocumentParser_parseHeader(t *testing.T) {
type testCase struct {
expDoc func() *Document
diff --git a/document_test.go b/document_test.go
index fcc95db..2a5fc73 100644
--- a/document_test.go
+++ b/document_test.go
@@ -4,8 +4,11 @@
package asciidoctor
import (
+ "bytes"
"os"
"testing"
+
+ "github.com/shuLhan/share/lib/test"
)
func TestOpen(t *testing.T) {
@@ -31,3 +34,235 @@ func TestOpen(t *testing.T) {
t.Fatal(err)
}
}
+
+func TestParse_metaDocTitle(t *testing.T) {
+ type testCase struct {
+ content string
+ }
+
+ var (
+ expHTML string = `
+<div class="paragraph">
+<p>Abc begins on a bleary Monday morning.</p>
+</div>`
+ )
+
+ var cases = []testCase{{
+ content: `= Abc
+
+{doctitle} begins on a bleary Monday morning.`,
+ }, {
+ content: `:doctitle: Abc
+
+{doctitle} begins on a bleary Monday morning.`,
+ }}
+
+ var (
+ doc *Document
+ c testCase
+ buf bytes.Buffer
+ err error
+ )
+
+ for _, c = range cases {
+ doc = Parse([]byte(c.content))
+ buf.Reset()
+ err = doc.ToHTMLEmbedded(&buf)
+ if err != nil {
+ t.Fatal(err)
+ }
+ test.Assert(t, "", expHTML, buf.String())
+ }
+}
+
+func TestParse_metaShowTitle(t *testing.T) {
+ type testCase struct {
+ desc string
+ content string
+ expHTML string
+ }
+
+ var cases = []testCase{{
+ desc: "default",
+ content: `= Abc`,
+ expHTML: `
+<div id="header">
+<h1>Abc</h1>
+<div class="details">
+</div>
+</div>
+<div id="content">
+<div id="preamble">
+<div class="sectionbody">
+</div>
+</div>
+</div>
+<div id="footer">
+<div id="footer-text">
+</div>
+</div>`,
+ }, {
+ desc: "with showtitle!",
+ content: `= Abc
+:showtitle!:`,
+ expHTML: `
+<div id="header">
+<div class="details">
+</div>
+</div>
+<div id="content">
+<div id="preamble">
+<div class="sectionbody">
+</div>
+</div>
+</div>
+<div id="footer">
+<div id="footer-text">
+</div>
+</div>`,
+ }}
+
+ var (
+ doc *Document
+ buf bytes.Buffer
+ c testCase
+ err error
+ )
+
+ for _, c = range cases {
+ doc = Parse([]byte(c.content))
+ buf.Reset()
+ err = doc.ToHTMLBody(&buf)
+ if err != nil {
+ t.Fatal(err)
+ }
+ test.Assert(t, c.desc, c.expHTML, buf.String())
+ }
+}
+
+func TestParse_document_title(t *testing.T) {
+ type testCase struct {
+ content string
+ expString string
+ exp DocumentTitle
+ }
+
+ var cases = []testCase{{
+ content: `= Main: sub`,
+ exp: DocumentTitle{
+ Main: "Main",
+ Sub: "sub",
+ sep: defTitleSeparator,
+ },
+ expString: "Main: sub",
+ }, {
+ // Without space after separator
+ content: `= Main:sub`,
+ exp: DocumentTitle{
+ Main: "Main:sub",
+ sep: defTitleSeparator,
+ },
+ expString: "Main:sub",
+ }, {
+ // With multiple separator after separator
+ content: `= a: b: c`,
+ exp: DocumentTitle{
+ Main: "a: b",
+ Sub: "c",
+ sep: defTitleSeparator,
+ },
+ expString: "a: b: c",
+ }, {
+ // With custom separator.
+ content: `:title-separator: x
+= Mainx sub`,
+ exp: DocumentTitle{
+ Main: "Main",
+ Sub: "sub",
+ sep: 'x',
+ },
+ expString: "Mainx sub",
+ }}
+
+ var (
+ c testCase
+ got *Document
+ )
+
+ for _, c = range cases {
+ got = Parse([]byte(c.content))
+ test.Assert(t, "Main", c.exp.Main, got.Title.Main)
+ test.Assert(t, "Sub", c.exp.Sub, got.Title.Sub)
+ test.Assert(t, "sep", c.exp.sep, got.Title.sep)
+ test.Assert(t, "String", c.expString, got.Title.String())
+ }
+}
+
+func TestParse_author(t *testing.T) {
+ type testCase struct {
+ desc string
+ content string
+ exp []*Author
+ }
+
+ var cases = []testCase{{
+ desc: "single author",
+ content: `= T
+A B`,
+ exp: []*Author{{
+ FirstName: "A",
+ LastName: "B",
+ Initials: "AB",
+ }},
+ }, {
+ desc: "single author with email",
+ content: `= T
+A B <a@b>`,
+ exp: []*Author{{
+ FirstName: "A",
+ LastName: "B",
+ Initials: "AB",
+ Email: "a@b",
+ }},
+ }, {
+ desc: "multiple authors",
+ content: `= T
+A B <a@b>; C <c@c>; D e_f G <>;`,
+ exp: []*Author{{
+ FirstName: "A",
+ LastName: "B",
+ Initials: "AB",
+ Email: "a@b",
+ }, {
+ FirstName: "C",
+ Initials: "C",
+ Email: "c@c",
+ }, {
+ FirstName: "D",
+ MiddleName: "e f",
+ LastName: "G",
+ Initials: "DeG",
+ }},
+ }, {
+ desc: "meta author",
+ content: `= T
+:author: A B
+:email: a@b`,
+ exp: []*Author{{
+ FirstName: "A",
+ LastName: "B",
+ Initials: "AB",
+ Email: "a@b",
+ }},
+ }}
+
+ var (
+ c testCase
+ got *Document
+ )
+
+ for _, c = range cases {
+ got = Parse([]byte(c.content))
+ test.Assert(t, c.desc, c.exp, got.Authors)
+ }
+}