Skip to content

Commit

Permalink
feat(parser/renderer): parse and render inline images (#17)
Browse files Browse the repository at this point in the history
also reorganize some tests by adding some contexts and
renaming the `describe` part

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon authored Aug 24, 2017
1 parent c499d94 commit 65f8ac7
Show file tree
Hide file tree
Showing 17 changed files with 835 additions and 592 deletions.
21 changes: 13 additions & 8 deletions parser/asciidoc-grammar.peg
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ Paragraph <- metadata:(MetadataElement)* lines:(InlineContent)+ {
}

// an inline content element may begin and end with spaces,
// but it must contain at least a quoted text, an external link or a word
InlineContent <- elements:(WS* (QuotedText / ExternalLink / Word) WS*)+ EOL {
// but it must contain at least an image, a quoted text, an external link or a word
InlineContent <- elements:(WS* (InlineImage / QuotedText / ExternalLink / Word) WS*)+ EOL {
return types.NewInlineContent(c.text, elements.([]interface{}))
}

Expand Down Expand Up @@ -168,15 +168,20 @@ ExternalLink <- url:(URL_SCHEME URL) text:('[' (URL_TEXT)* ']')? {
// ------------------------------------------
BlockImage <- metadata:(MetadataElement)* image:BlockImageMacro WS* EOL {
// here we can ignore the blank line in the returned element
return types.NewBlockImage(c.text, *image.(*types.BlockImageMacro), metadata.([]interface{}))
return types.NewBlockImage(c.text, *image.(*types.ImageMacro), metadata.([]interface{}))
}

BlockImageMacro <- "image::" path:(URL) '[' attributes:(URL_TEXT?) ']' {
if attributes != nil {
attrs := attributes.(string)
return types.NewBlockImageMacro(c.text, path.(string), &attrs)
}
return types.NewBlockImageMacro(c.text, path.(string), nil)
return types.NewImageMacro(c.text, path.(string), attributes)
}

InlineImage <- image:InlineImageMacro {
// here we can ignore the blank line in the returned element
return types.NewInlineImage(c.text, *image.(*types.ImageMacro))
}

InlineImageMacro <- "image:" !":" path:(URL) '[' attributes:(URL_TEXT?) ']' {
return types.NewImageMacro(c.text, path.(string), attributes)
}

// ------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions parser/asciidoc_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ var _ = Describe("Parsing content", func() {
})

func verify(t GinkgoTInterface, expectedDocument *types.Document, content string) {
log.Debugf("processing:\n%s", content)
log.Debugf("processing: %s", content)
reader := strings.NewReader(content)
result, err := ParseReader("", reader)
if err != nil {
Expand All @@ -83,7 +83,7 @@ func verify(t GinkgoTInterface, expectedDocument *types.Document, content string
require.Nil(t, err)
actualDocument := result.(*types.Document)
t.Logf("actual document structure: %+v", actualDocument.Elements)
t.Logf("actual document: %s", actualDocument.String(0))
t.Logf("expected document: %s", expectedDocument.String(0))
t.Logf("actual document: `%s`", actualDocument.String(0))
t.Logf("expected document: `%s`", expectedDocument.String(0))
assert.EqualValues(t, *expectedDocument, *actualDocument)
}
2 changes: 1 addition & 1 deletion parser/blank_line_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
. "github.com/onsi/ginkgo"
)

var _ = Describe("Rendering Blank lines", func() {
var _ = Describe("Blank lines", func() {
It("blank line between 2 paragraphs", func() {
actualDocument := `first paragraph
Expand Down
159 changes: 0 additions & 159 deletions parser/block_image_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion parser/delimited_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
. "github.com/onsi/ginkgo"
)

var _ = Describe("Parsing Delimited Blocks", func() {
var _ = Describe("Delimited Blocks", func() {

It("delimited source block with single line", func() {
content := "some source code"
Expand Down
2 changes: 1 addition & 1 deletion parser/external_link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
. "github.com/onsi/ginkgo"
)

var _ = Describe("Parsing External Links", func() {
var _ = Describe("External Links", func() {

It("external link", func() {
actualContent := "a link to https://foo.bar"
Expand Down
Loading

0 comments on commit 65f8ac7

Please sign in to comment.