Skip to content

Commit

Permalink
Add support for revealjs
Browse files Browse the repository at this point in the history
  • Loading branch information
owickstrom committed Dec 26, 2017
1 parent 63e1ff2 commit 4ae9e70
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 30 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Currently, the following output formats are supported:
- HTML (`html` and `html5`)
- LaTeX (`latex` and `beamer`)
- GitHub-Flavored Markdown (`markdown_github`)
- RevealJS (`revealjs`)

### Syntax

Expand Down Expand Up @@ -193,6 +194,9 @@ pandoc --filter pandoc-emphasize-code input.md output.html
Changelog
---------

- **0.2.1**
- Support `revealjs` output
- Use `<mark>` for HTML5 and RevealJS, `<em>` for HTML and GFM
- **0.2.0**
- Use Lucid to render HTML, fixes issue \#1
- **0.1.1**
Expand Down
4 changes: 4 additions & 0 deletions README.src.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Currently, the following output formats are supported:
* HTML (`html` and `html5`)
* LaTeX (`latex` and `beamer`)
* GitHub-Flavored Markdown (`markdown_github`)
* RevealJS (`revealjs`)

### Syntax

Expand Down Expand Up @@ -211,6 +212,9 @@ pandoc --filter pandoc-emphasize-code input.md output.html

## Changelog

* **0.2.1**
- Support `revealjs` output
- Use `<mark>` for HTML5 and RevealJS, `<em>` for HTML and GFM
* **0.2.0**
- Use Lucid to render HTML, fixes issue #1
* **0.1.1**
Expand Down
10 changes: 8 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,14 @@ <h2 id="usage">Usage</h2>
<p>Often when working with code examples in documentation, printed or web hosted, or in presentation slideshows, you might want to emphasize parts of a code snippet.</p>
<p>You can get away with manually writing the target markup, in LaTeX or raw HTML, but if you want to render the same document in multiple output formats, this gets really tedious. Also, having to write the markup by hand can be error prone.</p>
<p>This filter lets you specify <em>ranges</em> of a code block to emphasize, and have the filter generate the appropriate markup for you. It recognizes code blocks with the <code>emphasize</code> attribute present:</p>
<pre><code>```{.haskell <em>emphasize=2:3-2:14,3:3-3:12</em>}<br>myFunc = do<br> newStuffHere<br> andThisToo notThis<br> notSoRelevant<br>```</code></pre>
<pre><code>```{.haskell <mark>emphasize=2:3-2:14,3:3-3:12</mark>}<br>myFunc = do<br> newStuffHere<br> andThisToo notThis<br> notSoRelevant<br>```</code></pre>
<p>In the example above, the identifier <code>newStuffHere</code> and <code>andThisToo</code> will be emphasized.</p>
<p>Currently, the following output formats are supported:</p>
<ul>
<li>HTML (<code>html</code> and <code>html5</code>)</li>
<li>LaTeX (<code>latex</code> and <code>beamer</code>)</li>
<li>GitHub-Flavored Markdown (<code>markdown_github</code>)</li>
<li>RevealJS (<code>revealjs</code>)</li>
</ul>
<h3 id="syntax">Syntax</h3>
<p>The value of the <code>emphasize</code> attribute is a comma-separated list of <em>ranges</em>. A <em>range</em> consists of two positions, separated by a dash. A <em>position</em> consists of a <em>line number</em> and a <em>column number</em>, separated by a colon.</p>
Expand Down Expand Up @@ -118,7 +119,7 @@ <h3 id="rendering-to-html">Rendering to HTML</h3>
<span class="kw">font-style:</span> <span class="dt">normal</span><span class="kw">;</span>
<span class="kw">}</span></code></pre></div>
<p>By default, if no custom styling is applied, emphasized ranges in HTML will be rendered in italic type. With the CSS rule from above, it will instead look something like this:</p>
<pre class="haskell"><code>myFunc = do<br> <em>newStuffHere</em><br> <em>andThisToo</em> notThis<br> notSoRelevant</code></pre>
<pre class="haskell"><code>myFunc = do<br> <mark>newStuffHere</mark><br> <mark>andThisToo</mark> notThis<br> notSoRelevant</code></pre>
<p>Note that the there is no additional syntax highlighting when emphasizing code and rendering to HTML, as there is no way to use Pandoc's highlighter and embed custom HTML tags. You might be able to add that using a Javascript highlighter running on the client.</p>
<h3 id="rendering-with-latex">Rendering with LaTeX</h3>
<p>When rendering using LaTeX, two things are required:</p>
Expand Down Expand Up @@ -177,6 +178,11 @@ <h2 id="run">Run</h2>
<div class="sourceCode"><pre class="sourceCode sh"><code class="sourceCode bash"><span class="ex">pandoc</span> --filter pandoc-emphasize-code input.md output.html</code></pre></div>
<h2 id="changelog">Changelog</h2>
<ul>
<li><strong>0.2.1</strong>
<ul>
<li>Support <code>revealjs</code> output</li>
<li>Use <code>&lt;mark&gt;</code> for HTML5 and RevealJS, <code>&lt;em&gt;</code> for HTML and GFM</li>
</ul></li>
<li><strong>0.2.0</strong>
<ul>
<li>Use Lucid to render HTML, fixes issue #1</li>
Expand Down
Binary file modified docs/pandoc-emphasize-code.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion src/Text/Pandoc/Filter/EmphasizeCode.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ toRenderer ::
Pandoc.Format -> Maybe (Pandoc.Attr -> EmphasizedLines -> Pandoc.Block)
toRenderer f
| f `elem` ["html", "markdown_github"] = Just (renderEmphasized (Html Em))
| f == "html5" = Just (renderEmphasized (Html Mark))
| f `elem` ["html5", "revealjs"] = Just (renderEmphasized (Html Mark))
| f == "latex" = Just (renderEmphasized Latex)
| f == "beamer" = Just (renderEmphasized Latex)
| otherwise = Nothing
Expand Down
55 changes: 28 additions & 27 deletions test/Text/Pandoc/Filter/EmphasizeCodeTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@ import Test.Tasty.Hspec
import qualified Text.Pandoc.Filter.EmphasizeCode as Filter
import Text.Pandoc.JSON

singleRangeHtmlMark :: Block
singleRangeHtmlMark =
RawBlock
"html"
(mconcat
[ "<pre class=\"my-lang\"><code>hello world<br>"
, "hej <mark>världen</mark><br>"
, "<mark>hallo</mark> welt<br>"
, "hei verden</code></pre>"
])

singleRangeHtmlEm :: Block
singleRangeHtmlEm =
RawBlock
"html"
(mconcat
[ "<pre class=\"my-lang\"><code>hello world<br>"
, "hej <em>världen</em><br>"
, "<em>hallo</em> welt<br>"
, "hei verden</code></pre>"
])

emphasizeCode :: Format -> String -> IO Block
emphasizeCode format ranges =
Filter.emphasizeCode
Expand All @@ -18,15 +40,7 @@ emphasizeCode format ranges =

spec_emphasizeCode = do
it "emphasizes HTML and a single range" $
emphasizeCode "html5" "2:5-3:5" `shouldReturn`
RawBlock
"html"
(mconcat
[ "<pre class=\"my-lang\"><code>hello world<br>"
, "hej <mark>världen</mark><br>"
, "<mark>hallo</mark> welt<br>"
, "hei verden</code></pre>"
])
emphasizeCode "html5" "2:5-3:5" `shouldReturn` singleRangeHtmlMark
it "emphasizes HTML and a single range over multiple lines" $
emphasizeCode "html5" "2:5-4:3" `shouldReturn`
RawBlock
Expand All @@ -47,26 +61,12 @@ spec_emphasizeCode = do
, "<mark>hallo</mark> welt<br>"
, "hei verden</code></pre>"
])
it "emphasizes RevealJS HTML using <mark>" $
emphasizeCode "revealjs" "2:5-3:5" `shouldReturn` singleRangeHtmlMark
it "emphasizes HTML4 using <em>" $
emphasizeCode "html" "2:5-3:5" `shouldReturn`
RawBlock
"html"
(mconcat
[ "<pre class=\"my-lang\"><code>hello world<br>"
, "hej <em>världen</em><br>"
, "<em>hallo</em> welt<br>"
, "hei verden</code></pre>"
])
emphasizeCode "html" "2:5-3:5" `shouldReturn` singleRangeHtmlEm
it "emphasizes markdown_github using <em>" $
emphasizeCode "markdown_github" "2:5-3:5" `shouldReturn`
RawBlock
"html"
(mconcat
[ "<pre class=\"my-lang\"><code>hello world<br>"
, "hej <em>världen</em><br>"
, "<em>hallo</em> welt<br>"
, "hei verden</code></pre>"
])
emphasizeCode "markdown_github" "2:5-3:5" `shouldReturn` singleRangeHtmlEm
it "emphasizes latex and multiple ranges" $
emphasizeCode "latex" "1:1-1:5,2:5-3:5" `shouldReturn`
RawBlock
Expand All @@ -79,4 +79,5 @@ spec_emphasizeCode = do
, "hei verden\n"
, "\\end{lstlisting}\n"
])

{-# ANN module ("HLint: ignore Use camelCase" :: String) #-}

0 comments on commit 4ae9e70

Please sign in to comment.