Is it possible to set in the metadata of a file the language to use to highlight code blocks ? I rarely switch programming languages inside the same document.
I solved that particular problem by using a small filter script called ozify.hs (to force any unidentified code to the oz language)
Pass the script to the --filter option of pandoc.
The filter adds the "oz" tag to any untagged code (block and inline).
Of course this may require adaptations if you already have other labels hanging around.
#!/usr/bin/env runhaskell
-- ozify.hs
import Text.Pandoc
import Text.Pandoc.JSON
main :: IO ()
main = toJSONFilter ozify
ozify :: Block -> Block
ozify = bottomUp ozifyInline . bottomUp ozifyBlock
ozifyBlock (CodeBlock (id,[],keys) code) = CodeBlock (id, ["oz"], keys) code
ozifyBlock x = x
ozifyInline (Code (id,[],keys) code) = Code (id, ["oz"], keys) code
ozifyInline x = x
You can set the class for all indented code blocks, so these
are highlighted. --indented-code-classes=CLASS
+++ Lorenzo Bercelli [Apr 21 15 14:04 ]:
Is it possible to set in the metadata of a file the language to use to highlight code blocks ? I rarely switch programming languages inside the same document.
Reply to this email directly or view it on GitHub:
https://github.com/jgm/pandoc/issues/2104
thanks for the quick reply, this doesn't work for fenced code blocks though. Would be neat to extend it so that it does.
You could write a simple pandoc filter that adds the class attribute to all code
blocks.
+++ Lorenzo Bercelli [Apr 22 15 02:43 ]:
thanks for the quick reply, this doesn't work for fenced code blocks though. Would be neat to extend it so that it does.
Reply to this email directly or view it on GitHub:
https://github.com/jgm/pandoc/issues/2104#issuecomment-95101642
Is there a reason for --indented-code-classes to not work for all code blocks?
The thought was that it might be useful to have a way of getting a code block without any class, even if you're using --indented-code-classes to specify a default class for indented blocks. Currently you can use a fenced block for this.
I believe this issue could be re-opened (as a "feature request", maybe): having a way of specifying the language attribute(s) for all the code (indented, fenced with ` or ~, inlined) in a document would be a time-saver.
I just came across this scenario today as well. I want to cause to highlight a large number of references in `backticks` with a particular code class so as to emphasize the references, versus specifying each instance with a, e.g. {.blah}.
Use case for context- have a large working document that documents variables used for a project. Would like to make the variable references stand out by highlighting.
Aside, trying --indented-code-classes seems to override the explicitly set values rendering no highlighting at all.
Aside, trying --indented-code-classes seems to override the explicitly set values rendering no highlighting at all.
You may want to report that as a bug?
Aside, trying --indented-code-classes seems to override the explicitly set values rendering no highlighting at all.
You may want to report that as a bug?
It is interesting, because if- currently- using --indented-code-classes=X is intended to not provide styling for `backticked` code references, then the behavior shouldn't be to override/affect explicitly-defined references when calling --indented-code-classes.
Not sure I would refer to it as a bug but interesting to consider because if it isn't designed to affect/benefit `backtick` references, then why is it affecting them at all?
Clarifying and correcting myself- it does not appear that specifying --indented-code-classes=X at command line overrides explicitly-set code classes inline, e.g. `my example`{.lua}. Not sure how my initial test mislead me.
So, the input is:
--indented-code-classes=X or --default-code-classes=X to be able to preset a formatting syntax highlight for "bare" backtick references.... $(my_example) ... resulting in $(my_example){.lua} if --idented-code-classe=lua was set at command line or via defaults.
: having a way of specifying the language attribute(s) for all the code (indented, fenced with ` or ~, inlined) in a document would be a time-saver.
Create a file default-code-class.lua with these contents:
local default_code_classes = {}
function add_default_code_class(el)
if #(el.classes) == 0 then
el.classes = default_code_classes
return el
end
end
function get_default_code_class(meta)
if meta['default-code-class'] then
default_code_classes = {pandoc.utils.stringify(meta['default-code-class'])}
end
end
return {{Meta = get_default_code_class},
{Code = add_default_code_class},
{CodeBlock = add_default_code_class}}
Now you can use --lua-filter default-code-class.lua -M default-code-class=C on your command line to set the default code class to C.
That's perfect (except that the comment in lua starts with -- and not with %).
I have two questions:
Test:
int max(int num1, int num2) {return num1;}
By defaut, the code is presented as C code: `int`
~~~
int max(int num1, int num2) {return num1;}
~~~
But if explicitely switch to another langage, it is respected: `NULL`{.sql}
~~~{.sql}
SELECT * FROM TABLE WHERE 1 = 1;
~~~
produces as expected, when compiled with
pandoc --lua-filter default-code-class.lua -M default-code-class=C min.md -o min.html
