Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting the language attribute for code blocks for a whole document once #2104

Closed
Bercio opened this issue Apr 21, 2015 · 13 comments
Closed

Comments

@Bercio
Copy link

Bercio commented Apr 21, 2015

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.

@layus
Copy link

layus commented Apr 21, 2015

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

@jgm
Copy link
Owner

jgm commented Apr 21, 2015

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:
#2104

@Bercio
Copy link
Author

Bercio commented Apr 22, 2015

thanks for the quick reply, this doesn't work for fenced code blocks though. Would be neat to extend it so that it does.

@jgm
Copy link
Owner

jgm commented Apr 22, 2015

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:
#2104 (comment)

@mpickering
Copy link
Collaborator

Is there a reason for --indented-code-classes to not work for all code blocks?

@jgm
Copy link
Owner

jgm commented May 9, 2015

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.

@aubertc
Copy link

aubertc commented Feb 29, 2020

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.

@jaybe-jekyll
Copy link

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.

@aubertc
Copy link

aubertc commented Mar 6, 2020

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?

@jaybe-jekyll
Copy link

jaybe-jekyll commented Mar 6, 2020

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?

@jaybe-jekyll
Copy link

jaybe-jekyll commented Mar 6, 2020

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:

  • It would be nice if we could specify, e.g. --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.

@jgm
Copy link
Owner

jgm commented Mar 6, 2020

: 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.

@aubertc
Copy link

aubertc commented Mar 6, 2020

That's perfect (except that the comment in lua starts with -- and not with %).

I have two questions:

  • Any plans on integrating it into the main branch?
  • Can you specify multiple classes at the same time? I could not find a syntax or a tweak to the filter that works.

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

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants