public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
* Panflute filter: wrapping code block with docx custom style without losing syntax highlighting
@ 2020-05-15 22:49 Vittorio Romeo
       [not found] ` <7f65d550-17ce-474a-b830-31ff8b1d78b3-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  0 siblings, 1 reply; 2+ messages in thread
From: Vittorio Romeo @ 2020-05-15 22:49 UTC (permalink / raw)
  To: pandoc-discuss


[-- Attachment #1.1: Type: text/plain, Size: 1442 bytes --]

Hello, I am trying to write a filter to find all code blocks and wrap them 
into a custom `.docx` style. This is what I tried:

if type(elem) == pf.CodeBlock:
        return pf.Div(elem, attributes={'custom-style': 'Source Code 
Boxed'})

However, that doesn't work - it seems that the syntax highlighting styles 
override my desired `'Source Code Boxed'` style. I found a workaround, but 
it is ugly, and loses all the syntax highlighting:

if type(elem) == pf.CodeBlock:
    elemStr = pf.stringify(elem)
    lines = [pf.LineItem(pf.Str(x)) for x in elemStr.splitlines()]
    return pf.Div(pf.LineBlock(*lines), 
            attributes={ 'custom-style': 'Source Code Boxed'})

The above works (it surrounds my code block in a box in my final `.docx` 
output), but all syntax highlighting is lost.

**Is there a way to achieve my desired result?** I would the code being 
surrounded in a box (thanks to my `'Source Code Boxed'` style), but still 
retain its syntax highlighting.

I have the same problem with TableCell, by the way.

-- 
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/7f65d550-17ce-474a-b830-31ff8b1d78b3%40googlegroups.com.

[-- Attachment #1.2: Type: text/html, Size: 2521 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Panflute filter: wrapping code block with docx custom style without losing syntax highlighting
       [not found] ` <7f65d550-17ce-474a-b830-31ff8b1d78b3-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2020-06-03  8:16   ` K4zuki
  0 siblings, 0 replies; 2+ messages in thread
From: K4zuki @ 2020-06-03  8:16 UTC (permalink / raw)
  To: pandoc-discuss


[-- Attachment #1.1: Type: text/plain, Size: 2231 bytes --]

Hello,

I tried similar thing also by filters but finally chose two-pass processing 
cooperating with python-docx package:

The 1st pass converts from your source to docx by pandoc, using a custom 
template that contains the "Source Code Boxed" style. The standard "Source 
Code" style is applied for CodeBlock at this moment.
The 2nd pass finds paragraphs that "Source Code" paragraph style is applied 
and replace by "Source Code Boxed" style.

See also:
https://github.com/K4zuki/docx-core-property-writer/blob/78688b7aae93244511cafb15f9213c4cb294b65b/docx_coreprop_writer/__init__.py#L362

Hope this helps,
K4zuki / Kazuki

2020年5月16日土曜日 7時49分16秒 UTC+9 Vittorio Romeo:
>
> Hello, I am trying to write a filter to find all code blocks and wrap them 
> into a custom `.docx` style. This is what I tried:
>
> if type(elem) == pf.CodeBlock:
>         return pf.Div(elem, attributes={'custom-style': 'Source Code 
> Boxed'})
>
> However, that doesn't work - it seems that the syntax highlighting styles 
> override my desired `'Source Code Boxed'` style. I found a workaround, but 
> it is ugly, and loses all the syntax highlighting:
>
> if type(elem) == pf.CodeBlock:
>     elemStr = pf.stringify(elem)
>     lines = [pf.LineItem(pf.Str(x)) for x in elemStr.splitlines()]
>     return pf.Div(pf.LineBlock(*lines), 
>             attributes={ 'custom-style': 'Source Code Boxed'})
>
> The above works (it surrounds my code block in a box in my final `.docx` 
> output), but all syntax highlighting is lost.
>
> **Is there a way to achieve my desired result?** I would the code being 
> surrounded in a box (thanks to my `'Source Code Boxed'` style), but still 
> retain its syntax highlighting.
>
> I have the same problem with TableCell, by the way.
>

-- 
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/5eb81ba3-1b07-4207-8b73-8cb95ded3167%40googlegroups.com.

[-- Attachment #1.2: Type: text/html, Size: 3522 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-06-03  8:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-15 22:49 Panflute filter: wrapping code block with docx custom style without losing syntax highlighting Vittorio Romeo
     [not found] ` <7f65d550-17ce-474a-b830-31ff8b1d78b3-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2020-06-03  8:16   ` K4zuki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).