Skip to content

Commit

Permalink
Use latest skylighting; ensure no duplicate ids on code lines.
Browse files Browse the repository at this point in the history
The line identifiers are built using the code block's identifier
as a prefix. If the code block has null identifier, we use
"cb1", "cb2", etc.

Closes #4031.
  • Loading branch information
jgm committed Nov 2, 2017
1 parent 5df2722 commit 856587f
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pandoc.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ library
tagsoup >= 0.13.7 && < 0.15,
base64-bytestring >= 0.1 && < 1.1,
zlib >= 0.5 && < 0.7,
skylighting >= 0.4.2 && <0.5,
skylighting >= 0.4.3 && <0.5,
data-default >= 0.4 && < 0.8,
temporary >= 1.1 && < 1.3,
blaze-html >= 0.5 && < 0.10,
Expand Down
7 changes: 5 additions & 2 deletions src/Text/Pandoc/Highlighting.hs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,15 @@ highlight :: SyntaxMap
-> Attr -- ^ Attributes of the CodeBlock
-> String -- ^ Raw contents of the CodeBlock
-> Either String a
highlight syntaxmap formatter (_, classes, keyvals) rawCode =
highlight syntaxmap formatter (ident, classes, keyvals) rawCode =
let firstNum = fromMaybe 1 (safeRead (fromMaybe "1" $ lookup "startFrom" keyvals))
fmtOpts = defaultFormatOpts{
startNumber = firstNum,
numberLines = any (`elem`
["number","numberLines", "number-lines"]) classes }
["number","numberLines", "number-lines"]) classes,
lineIdPrefix = if null ident
then mempty
else T.pack (ident ++ "-") }
tokenizeOpts = TokenizerConfig{ syntaxMap = syntaxmap
, traceOutput = False }
classes' = map T.pack classes
Expand Down
14 changes: 11 additions & 3 deletions src/Text/Pandoc/Writers/HTML.hs
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,16 @@ data WriterState = WriterState
, stHtml5 :: Bool -- ^ Use HTML5
, stEPUBVersion :: Maybe EPUBVersion -- ^ EPUB version if for epub
, stSlideVariant :: HTMLSlideVariant
, stCodeBlockNum :: Int -- ^ Number of code block
}

defaultWriterState :: WriterState
defaultWriterState = WriterState {stNotes= [], stMath = False, stQuotes = False,
stHighlighting = False, stSecNum = [],
stElement = False, stHtml5 = False,
stEPUBVersion = Nothing,
stSlideVariant = NoSlides}
stSlideVariant = NoSlides,
stCodeBlockNum = 0}

-- Helpers to render HTML with the appropriate function.

Expand Down Expand Up @@ -703,6 +705,12 @@ blockToHtml _ HorizontalRule = do
html5 <- gets stHtml5
return $ if html5 then H5.hr else H.hr
blockToHtml opts (CodeBlock (id',classes,keyvals) rawCode) = do
id'' <- if null id'
then do
modify $ \st -> st{ stCodeBlockNum = stCodeBlockNum st + 1 }
codeblocknum <- gets stCodeBlockNum
return ("cb" ++ show codeblocknum)
else return id'
let tolhs = isEnabled Ext_literate_haskell opts &&
any (\c -> map toLower c == "haskell") classes &&
any (\c -> map toLower c == "literate") classes
Expand All @@ -716,7 +724,7 @@ blockToHtml opts (CodeBlock (id',classes,keyvals) rawCode) = do
else rawCode
hlCode = if isJust (writerHighlightStyle opts)
then highlight (writerSyntaxMap opts) formatHtmlBlock
(id',classes',keyvals) adjCode
(id'',classes',keyvals) adjCode
else Left ""
case hlCode of
Left msg -> do
Expand All @@ -725,7 +733,7 @@ blockToHtml opts (CodeBlock (id',classes,keyvals) rawCode) = do
addAttrs opts (id',classes,keyvals)
$ H.pre $ H.code $ toHtml adjCode
Right h -> modify (\st -> st{ stHighlighting = True }) >>
addAttrs opts (id',[],keyvals) h
addAttrs opts (id'',[],keyvals) h
blockToHtml opts (BlockQuote blocks) = do
-- in S5, treat list in blockquote specially
-- if default is incremental, make it nonincremental;
Expand Down
2 changes: 1 addition & 1 deletion stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ packages:
extra-deps:
- pandoc-types-1.17.2
- hslua-0.9.2
- skylighting-0.4.2
- skylighting-0.4.3
- cmark-gfm-0.1.1
- QuickCheck-2.10.0.1
- tasty-quickcheck-0.9.1
Expand Down
6 changes: 3 additions & 3 deletions test/lhs-test.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@
<h1 id="lhs-test">lhs test</h1>
<p><code>unsplit</code> is an arrow that takes a pair of values and combines them to
return a single value:</p>
<pre class="sourceCode literate haskell"><code class="sourceCode haskell"><div class="sourceLine" id="1" href="#1" data-line-number="1"><span class="ot">unsplit ::</span> (<span class="dt">Arrow</span> a) <span class="ot">=&gt;</span> (b <span class="ot">-&gt;</span> c <span class="ot">-&gt;</span> d) <span class="ot">-&gt;</span> a (b, c) d</div>
<div class="sourceLine" id="2" href="#2" data-line-number="2">unsplit <span class="fu">=</span> arr <span class="fu">.</span> uncurry</div>
<div class="sourceLine" id="3" href="#3" data-line-number="3"> <span class="co">-- arr (\op (x,y) -&gt; x `op` y)</span></div></code></pre>
<pre class="sourceCode literate haskell" id="cb1"><code class="sourceCode haskell"><div class="sourceLine" id="cb1-1" data-line-number="cb1-1"><span class="ot">unsplit ::</span> (<span class="dt">Arrow</span> a) <span class="ot">=&gt;</span> (b <span class="ot">-&gt;</span> c <span class="ot">-&gt;</span> d) <span class="ot">-&gt;</span> a (b, c) d</div>
<div class="sourceLine" id="cb1-2" data-line-number="cb1-2">unsplit <span class="fu">=</span> arr <span class="fu">.</span> uncurry</div>
<div class="sourceLine" id="cb1-3" data-line-number="cb1-3"> <span class="co">-- arr (\op (x,y) -&gt; x `op` y)</span></div></code></pre>
<p><code>(***)</code> combines two arrows into a new arrow by running the two arrows on a
pair of values (one arrow on the first item of the pair and one arrow on the
second item of the pair).</p>
Expand Down
6 changes: 3 additions & 3 deletions test/lhs-test.html+lhs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Infor
<h1 id="lhs-test">lhs test</h1>
<p><code>unsplit</code> is an arrow that takes a pair of values and combines them to
return a single value:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><div class="sourceLine" id="1" href="#1" data-line-number="1"><span class="ot">&gt; unsplit ::</span> (<span class="dt">Arrow</span> a) <span class="ot">=&gt;</span> (b <span class="ot">-&gt;</span> c <span class="ot">-&gt;</span> d) <span class="ot">-&gt;</span> a (b, c) d</div>
<div class="sourceLine" id="2" href="#2" data-line-number="2"><span class="ot">&gt;</span> unsplit <span class="fu">=</span> arr <span class="fu">.</span> uncurry</div>
<div class="sourceLine" id="3" href="#3" data-line-number="3"><span class="ot">&gt;</span> <span class="co">-- arr (\op (x,y) -&gt; x `op` y)</span></div></code></pre>
<pre class="sourceCode literate literatehaskell" id="cb1"><code class="sourceCode literatehaskell"><div class="sourceLine" id="cb1-1" data-line-number="cb1-1"><span class="ot">&gt; unsplit ::</span> (<span class="dt">Arrow</span> a) <span class="ot">=&gt;</span> (b <span class="ot">-&gt;</span> c <span class="ot">-&gt;</span> d) <span class="ot">-&gt;</span> a (b, c) d</div>
<div class="sourceLine" id="cb1-2" data-line-number="cb1-2"><span class="ot">&gt;</span> unsplit <span class="fu">=</span> arr <span class="fu">.</span> uncurry</div>
<div class="sourceLine" id="cb1-3" data-line-number="cb1-3"><span class="ot">&gt;</span> <span class="co">-- arr (\op (x,y) -&gt; x `op` y)</span></div></code></pre>
<p><code>(***)</code> combines two arrows into a new arrow by running the two arrows on a
pair of values (one arrow on the first item of the pair and one arrow on the
second item of the pair).</p>
Expand Down

2 comments on commit 856587f

@HazemAM
Copy link

@HazemAM HazemAM commented on 856587f Nov 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jgm - Thanks for the fix. I tested it using the nightly builds. I just wanted to point out that the data-line-number attribute also contain the id/cb# prefix. I'm not sure if this is intended, but this will make the line numbers appear prefixed in the output too:

Prefixed line numbers in code blocks

I would suggest adding the prefix only to the id attribute, and leaving the data-line-number un-prefixed, regardless of the block number:

<div class="sourceLine" id="cb3-1" data-line-number="1">
    <!-- some code -->
</div>

Aside: I mentioned this in the #4031 issue after it closed, so I understand it may got buried. But I would happily open another issue for tracking if required.

@jgm
Copy link
Owner Author

@jgm jgm commented on 856587f Nov 5, 2017 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.