Implement inline export blocks

https://orgmode.org/manual/Quoting-HTML-tags.html
This commit is contained in:
Niklas Fasching 2020-04-16 14:56:43 +02:00
parent 3018ace8d0
commit 4b56810a65
6 changed files with 42 additions and 14 deletions

View file

@ -133,9 +133,16 @@ func (w *HTMLWriter) WriteBlock(b Block) {
}
func (w *HTMLWriter) WriteInlineBlock(b InlineBlock) {
content := w.blockContent(b.Name, b.Children)
content := w.blockContent(strings.ToUpper(b.Name), b.Children)
switch b.Name {
case "src":
lang := strings.ToLower(b.Parameters[0])
w.WriteString(fmt.Sprintf("<div class=\"src src-inline src-%s\">\n%s\n</div>", lang, content))
case "export":
if strings.ToLower(b.Parameters[0]) == "html" {
w.WriteString(content + "\n")
}
}
}
func (w *HTMLWriter) WriteDrawer(d Drawer) {

View file

@ -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):

View file

@ -98,6 +98,8 @@ func (w *OrgWriter) WriteBlock(b Block) {
}
func (w *OrgWriter) WriteInlineBlock(b InlineBlock) {
switch b.Name {
case "src":
w.WriteString(b.Name + "_" + b.Parameters[0])
if len(b.Parameters) > 1 {
w.WriteString("[" + strings.Join(b.Parameters[1:], " ") + "]")
@ -105,6 +107,11 @@ func (w *OrgWriter) WriteInlineBlock(b InlineBlock) {
w.WriteString("{")
WriteNodes(w, b.Children...)
w.WriteString("}")
case "export":
w.WriteString("@@" + b.Parameters[0] + ":")
WriteNodes(w, b.Children...)
w.WriteString("@@")
}
}
func (w *OrgWriter) WriteDrawer(d Drawer) {

View file

@ -44,12 +44,14 @@ links with slashes do not become <em>emphasis</em>: <a href="https://somelinksho
<li>
<p>
inline source blocks like <div class="src src-inline src-html">
<div class="highlight-inline">
<pre>
&lt;h1&gt;hello&lt;/h1&gt;
</pre>
</div>
<h1>hello</h1>
</div>
</p>
</li>
<li>
<p>
inline export blocks <h1>hello</h1>
</p>
</li>
<li>

View file

@ -9,6 +9,7 @@
- _underlined_ *bold* =verbatim= ~code~ +strikethrough+
- *bold string with an *asterisk inside*
- inline source blocks like src_html[:eval no]{<h1>hello</h1>}
- inline export blocks @@html:<h1>hello</h1>@@
- =multiline emphasis is
supported - and respects MaxEmphasisNewLines (default: 1)=
/so this

View file

@ -9,6 +9,7 @@
- _underlined_ *bold* =verbatim= ~code~ +strikethrough+
- *bold string with an *asterisk inside*
- inline source blocks like src_html[:eval no]{<h1>hello</h1>}
- inline export blocks @@html:<h1>hello</h1>@@
- =multiline emphasis is
supported - and respects MaxEmphasisNewLines (default: 1)=
/so this