Skip to content

Commit

Permalink
move CompileMarkdown to package
Browse files Browse the repository at this point in the history
  • Loading branch information
kovetskiy committed Apr 14, 2019
1 parent ada6f82 commit 5f582e5
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 84 deletions.
18 changes: 0 additions & 18 deletions macro_code.go
Original file line number Diff line number Diff line change
@@ -1,19 +1 @@
package main

import "fmt"

type MacroCode struct {
lang string
code []byte
}

func (code MacroCode) Render() string {
return fmt.Sprintf(
`<ac:structured-macro ac:name="code">`+
`<ac:parameter ac:name="language">%s</ac:parameter>`+
`<ac:parameter ac:name="collapse">false</ac:parameter>`+
`<ac:plain-text-body><![CDATA[%s]]></ac:plain-text-body>`+
`</ac:structured-macro>`,
code.lang, code.code,
)
}
49 changes: 1 addition & 48 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/BurntSushi/toml"
Expand All @@ -15,7 +14,6 @@ import (
"github.com/kovetskiy/mark/pkg/mark"
"github.com/reconquest/colorgful"
"github.com/reconquest/karma-go"
"github.com/russross/blackfriday"
"github.com/zazab/zhash"
)

Expand Down Expand Up @@ -140,7 +138,7 @@ func main() {
logger.Fatal(err)
}

htmlData := compileMarkdown(markdownData)
htmlData := mark.CompileMarkdown(markdownData)

if dryRun {
fmt.Println(string(htmlData))
Expand Down Expand Up @@ -227,51 +225,6 @@ func main() {

}

// compileMarkdown will replace tags like <ac:rich-tech-body> with escaped
// equivalent, because blackfriday markdown parser replaces that tags with
// <a href="ac:rich-text-body">ac:rich-text-body</a> for whatever reason.
func compileMarkdown(markdown []byte) []byte {
colon := regexp.MustCompile(`---BLACKFRIDAY-COLON---`)

tags := regexp.MustCompile(`<(/?\S+):(\S+)>`)

markdown = tags.ReplaceAll(
markdown,
[]byte(`<$1`+colon.String()+`$2>`),
)

renderer := ConfluenceRenderer{
blackfriday.HtmlRenderer(
blackfriday.HTML_USE_XHTML|
blackfriday.HTML_USE_SMARTYPANTS|
blackfriday.HTML_SMARTYPANTS_FRACTIONS|
blackfriday.HTML_SMARTYPANTS_DASHES|
blackfriday.HTML_SMARTYPANTS_LATEX_DASHES,
"", "",
),
}

html := blackfriday.MarkdownOptions(
markdown,
renderer,
blackfriday.Options{
Extensions: blackfriday.EXTENSION_NO_INTRA_EMPHASIS |
blackfriday.EXTENSION_TABLES |
blackfriday.EXTENSION_FENCED_CODE |
blackfriday.EXTENSION_AUTOLINK |
blackfriday.EXTENSION_STRIKETHROUGH |
blackfriday.EXTENSION_SPACE_HEADERS |
blackfriday.EXTENSION_HEADER_IDS |
blackfriday.EXTENSION_BACKSLASH_LINE_BREAK |
blackfriday.EXTENSION_DEFINITION_LISTS,
},
)

html = colon.ReplaceAll(html, []byte(`:`))

return html
}

func resolvePage(
api *confluence.API,
meta *mark.Meta,
Expand Down
82 changes: 82 additions & 0 deletions pkg/mark/markdown.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package mark

import (
"bytes"
"fmt"
"regexp"

"github.com/russross/blackfriday"
)

type ConfluenceRenderer struct {
blackfriday.Renderer
}

func (renderer ConfluenceRenderer) BlockCode(
out *bytes.Buffer,
text []byte,
lang string,
) {
out.WriteString(MacroCode{lang, text}.Render())
}

type MacroCode struct {
lang string
code []byte
}

func (code MacroCode) Render() string {
return fmt.Sprintf(
`<ac:structured-macro ac:name="code">`+
`<ac:parameter ac:name="language">%s</ac:parameter>`+
`<ac:parameter ac:name="collapse">false</ac:parameter>`+
`<ac:plain-text-body><![CDATA[%s]]></ac:plain-text-body>`+
`</ac:structured-macro>`,
code.lang, code.code,
)
}

// compileMarkdown will replace tags like <ac:rich-tech-body> with escaped
// equivalent, because blackfriday markdown parser replaces that tags with
// <a href="ac:rich-text-body">ac:rich-text-body</a> for whatever reason.
func CompileMarkdown(markdown []byte) []byte {
colon := regexp.MustCompile(`---BLACKFRIDAY-COLON---`)

tags := regexp.MustCompile(`<(/?\S+):(\S+)>`)

markdown = tags.ReplaceAll(
markdown,
[]byte(`<$1`+colon.String()+`$2>`),
)

renderer := ConfluenceRenderer{
blackfriday.HtmlRenderer(
blackfriday.HTML_USE_XHTML|
blackfriday.HTML_USE_SMARTYPANTS|
blackfriday.HTML_SMARTYPANTS_FRACTIONS|
blackfriday.HTML_SMARTYPANTS_DASHES|
blackfriday.HTML_SMARTYPANTS_LATEX_DASHES,
"", "",
),
}

html := blackfriday.MarkdownOptions(
markdown,
renderer,
blackfriday.Options{
Extensions: blackfriday.EXTENSION_NO_INTRA_EMPHASIS |
blackfriday.EXTENSION_TABLES |
blackfriday.EXTENSION_FENCED_CODE |
blackfriday.EXTENSION_AUTOLINK |
blackfriday.EXTENSION_STRIKETHROUGH |
blackfriday.EXTENSION_SPACE_HEADERS |
blackfriday.EXTENSION_HEADER_IDS |
blackfriday.EXTENSION_BACKSLASH_LINE_BREAK |
blackfriday.EXTENSION_DEFINITION_LISTS,
},
)

html = colon.ReplaceAll(html, []byte(`:`))

return html
}
18 changes: 0 additions & 18 deletions renderer.go
Original file line number Diff line number Diff line change
@@ -1,19 +1 @@
package main

import (
"bytes"

"github.com/russross/blackfriday"
)

type ConfluenceRenderer struct {
blackfriday.Renderer
}

func (renderer ConfluenceRenderer) BlockCode(
out *bytes.Buffer,
text []byte,
lang string,
) {
out.WriteString(MacroCode{lang, text}.Render())
}

0 comments on commit 5f582e5

Please sign in to comment.