inserting non-breaking space in code samples for html in email

33 views
Skip to first unread message

j...@saturnflyer.com

unread,
Apr 25, 2018, 3:28:57 PM4/25/18
to kramdown-users
I'm working with kramdown to generate code samples for email.

Email viewers tend to have trouble with formatting inside of <pre> elements so I'm looking to change any spaces in my code samples to non-breaking spaces.

Is there a way to hook into the fenced code block processing to change instances of a single space character to "&nbsp;"?
I want to end each line of code with a break element as well.

Have I overlooked a simple way to do this?

-Jim 

Thomas Leitner

unread,
Apr 26, 2018, 1:54:23 AM4/26/18
to j...@saturnflyer.com, kramdow...@googlegroups.com
On 2018-04-25 12:28 -0700 j...@saturnflyer.com wrote:
> Is there a way to hook into the fenced code block processing to
> change instances of a single space character to "&nbsp;"?
> I want to end each line of code with a break element as well.

If you want to only change code blocks created by the fenced code
blocks parser, you would need to derive a parser class from the
standard kramdown parser and override the #parse_codeblock_fenced
method.

However, you could also parse the document and then modify the abstract
syntax tree before outputting to HTML, see the [TOC converter][1] for a
possibility.

Finally, the third option would be to derive a new converter from the
HTML converter and override the #convert_codeblock method by
pre-processing `el.value` (the contents of the codeblock) with what you
need.

Best regards,
Thomas


[1]:
https://github.com/gettalong/kramdown/blob/master/lib/kramdown/converter/toc.rb
Reply all
Reply to author
Forward
0 new messages