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

Support Minted for LaTeX Code Block Conversion #93

Closed
gettalong opened this issue Nov 11, 2013 · 6 comments
Closed

Support Minted for LaTeX Code Block Conversion #93

gettalong opened this issue Nov 11, 2013 · 6 comments

Comments

@gettalong
Copy link
Owner

[from Rubyforge]

It's excellent that Kramdown already supports a solid, separate package like Listings for formatting code blocks whenconverting to LaTeX. However, I'd like to request that Minted be supported as well.

Minted uses the Pygmentize library for its conversion, and was built in response to some of the shortcomings of Listings.Tools like Emacs' Org mode provide a configuration option when exporting to LaTeX to choose between exporting code blocks as Listing environments or Minted environments.

Thank you for an excellent open source project.

@ghost ghost assigned gettalong Nov 11, 2013
@gettalong
Copy link
Owner Author

This could now be implemented as a generic syntax highlighter. And the LaTeX converter still needs to be adjusted to use the generic syntax highlighter implementation.

@l3kn
Copy link
Contributor

l3kn commented Apr 11, 2015

I would like to try my hand at this, but I am not sure what would be the best way.

Currently all converters in converter/syntax_highlighter seem to be html only.
Is there a way to guarantee a syntax_highlighter is called from the correct converter
(e.g. setting :syntax_highlighter to minted would not mess up html conversion)?

@gettalong
Copy link
Owner Author

That would be great!

No, there is currently no such provision. Providing a correct value to the syntax highlighter option is the job of the user.

However, I toying around with the idea of introducing mime types for the parsers and converters. If this is done, it should be possible to set a preferred syntax highlighter for a mime type instead of a specific converter.

So for the time being, this issue would just need a new syntax highlighter and the adaption of the LaTeX converter.

@l3kn
Copy link
Contributor

l3kn commented Apr 12, 2015

I forked the repo and changed the LaTeX converter use the highlight_code function (like the html converter).

But this way running kramdown test.md -o latex without specifying a --syntax-highlighter falls back to the default highlighter (coderay) and outputs a mix of LaTeX and html.

The best fix I could come up with would be to add a optional constant
SUPPORTED_CONVERTERS = [:Html] # or [:Latex, :Html, ...]
to the syntax highlighters and let converter.rb check if the current converter is supported*.

Should I open up a pull request with this changes first?

add_syntax_highlighter(kn_down) do |converter, text, lang, type, opts|
  require "kramdown/converter/syntax_highlighter/#{kn_down}"
  klass = ::Kramdown::Utils.deep_const_get("::Kramdown::Converter::SyntaxHighlighter::#{klass_name}")
  converter_name = converter.class.name.split('::').last.to_sym # maybe use strings?

  # Use || because SUPPORTED_CONVERTERS should be optional
  supported = klass::SUPPORTED_CONVERTERS.nil? || klass::SUPPORTED_CONVERTERS.member?(converter_name)

  if klass::AVAILABLE && supported
    add_syntax_highlighter(kn_down, klass)
  else
    add_syntax_highlighter(kn_down) {|*args| nil}
  end
    syntax_highlighter(kn_down).call(converter, text, lang, type, opts)
end

@gettalong
Copy link
Owner Author

The problem with using a constant like SUPPORTED_CONVERTERS is that custom HTML converters can't be used anymore with the syntax highlighters. The mime type approach I mentioned would circumvent this problem but I have to think a bit more about this.

Until such a facility is ready I think the best option would be to allow the LaTeX converter to optionally use exactly one specific syntax_highlighter, namely the one for minted. I.e. when syntax_highlighter is minted, the LaTeX converter should use the highlighting facility and otherwise should do its default thing.

@gettalong
Copy link
Owner Author

Thanks for your contribution, the changes are now in master.

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

No branches or pull requests

2 participants