From 4b56810a6561e8ec253fe97d6d6bdd2ab1b308f8 Mon Sep 17 00:00:00 2001 From: Niklas Fasching Date: Thu, 16 Apr 2020 14:56:43 +0200 Subject: [PATCH] Implement inline export blocks https://orgmode.org/manual/Quoting-HTML-tags.html --- org/html_writer.go | 13 ++++++++++--- org/inline.go | 10 ++++++++++ org/org_writer.go | 19 +++++++++++++------ org/testdata/inline.html | 12 +++++++----- org/testdata/inline.org | 1 + org/testdata/inline.pretty_org | 1 + 6 files changed, 42 insertions(+), 14 deletions(-) diff --git a/org/html_writer.go b/org/html_writer.go index 1f4bb9c..31efe18 100644 --- a/org/html_writer.go +++ b/org/html_writer.go @@ -133,9 +133,16 @@ func (w *HTMLWriter) WriteBlock(b Block) { } func (w *HTMLWriter) WriteInlineBlock(b InlineBlock) { - content := w.blockContent(b.Name, b.Children) - lang := strings.ToLower(b.Parameters[0]) - w.WriteString(fmt.Sprintf("
\n%s\n
", lang, content)) + content := w.blockContent(strings.ToUpper(b.Name), b.Children) + switch b.Name { + case "src": + lang := strings.ToLower(b.Parameters[0]) + w.WriteString(fmt.Sprintf("
\n%s\n
", lang, content)) + case "export": + if strings.ToLower(b.Parameters[0]) == "html" { + w.WriteString(content + "\n") + } + } } func (w *HTMLWriter) WriteDrawer(d Drawer) { diff --git a/org/inline.go b/org/inline.go index f0f90b4..aeebb5b 100644 --- a/org/inline.go +++ b/org/inline.go @@ -65,6 +65,7 @@ var footnoteRegexp = regexp.MustCompile(`^\[fn:([\w-]*?)(:(.*?))?\]`) var statisticsTokenRegexp = regexp.MustCompile(`^\[(\d+/\d+|\d+%)\]`) var latexFragmentRegexp = regexp.MustCompile(`(?s)^\\begin{(\w+)}(.*)\\end{(\w+)}`) var inlineBlockRegexp = regexp.MustCompile(`src_(\w+)(\[(.*)\])?{(.*)}`) +var inlineExportBlockRegexp = regexp.MustCompile(`@@(\w+):(.*?)@@`) var timestampFormat = "2006-01-02 Mon 15:04" var datestampFormat = "2006-01-02 Mon" @@ -85,6 +86,8 @@ func (d *Document) parseInline(input string) (nodes []Node) { consumed, node = d.parseSubOrSuperScript(input, current) case '_': rewind, consumed, node = d.parseSubScriptOrEmphasisOrInlineBlock(input, current) + case '@': + consumed, node = d.parseInlineExportBlock(input, current) case '*', '/', '+': consumed, node = d.parseEmphasis(input, current, false) case '=', '~': @@ -161,6 +164,13 @@ func (d *Document) parseInlineBlock(input string, start int) (int, int, Node) { return 0, 0, nil } +func (d *Document) parseInlineExportBlock(input string, start int) (int, Node) { + if m := inlineExportBlockRegexp.FindStringSubmatch(input[start:]); m != nil { + return len(m[0]), InlineBlock{"export", m[1:2], d.parseRawInline(m[2])} + } + return 0, nil +} + func (d *Document) parseExplicitLineBreakOrLatexFragment(input string, start int) (int, Node) { switch { case start+2 >= len(input): diff --git a/org/org_writer.go b/org/org_writer.go index 8bca476..b562cf3 100644 --- a/org/org_writer.go +++ b/org/org_writer.go @@ -98,13 +98,20 @@ func (w *OrgWriter) WriteBlock(b Block) { } func (w *OrgWriter) WriteInlineBlock(b InlineBlock) { - w.WriteString(b.Name + "_" + b.Parameters[0]) - if len(b.Parameters) > 1 { - w.WriteString("[" + strings.Join(b.Parameters[1:], " ") + "]") + switch b.Name { + case "src": + w.WriteString(b.Name + "_" + b.Parameters[0]) + if len(b.Parameters) > 1 { + w.WriteString("[" + strings.Join(b.Parameters[1:], " ") + "]") + } + w.WriteString("{") + WriteNodes(w, b.Children...) + w.WriteString("}") + case "export": + w.WriteString("@@" + b.Parameters[0] + ":") + WriteNodes(w, b.Children...) + w.WriteString("@@") } - w.WriteString("{") - WriteNodes(w, b.Children...) - w.WriteString("}") } func (w *OrgWriter) WriteDrawer(d Drawer) { diff --git a/org/testdata/inline.html b/org/testdata/inline.html index 3ba6474..2bcdf92 100644 --- a/org/testdata/inline.html +++ b/org/testdata/inline.html @@ -44,12 +44,14 @@ links with slashes do not become emphasis: -
-
-<h1>hello</h1>
-
-
+

hello

+

+ +
  • +

    +inline export blocks

    hello

    +

  • diff --git a/org/testdata/inline.org b/org/testdata/inline.org index dfd2b01..488d037 100644 --- a/org/testdata/inline.org +++ b/org/testdata/inline.org @@ -9,6 +9,7 @@ - _underlined_ *bold* =verbatim= ~code~ +strikethrough+ - *bold string with an *asterisk inside* - inline source blocks like src_html[:eval no]{

    hello

    } +- inline export blocks @@html:

    hello

    @@ - =multiline emphasis is supported - and respects MaxEmphasisNewLines (default: 1)= /so this diff --git a/org/testdata/inline.pretty_org b/org/testdata/inline.pretty_org index e1bd477..b11677c 100644 --- a/org/testdata/inline.pretty_org +++ b/org/testdata/inline.pretty_org @@ -9,6 +9,7 @@ - _underlined_ *bold* =verbatim= ~code~ +strikethrough+ - *bold string with an *asterisk inside* - inline source blocks like src_html[:eval no]{

    hello

    } +- inline export blocks @@html:

    hello

    @@ - =multiline emphasis is supported - and respects MaxEmphasisNewLines (default: 1)= /so this