ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Mojca Miklavec <mojca.miklavec.lists@gmail.com>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Adding syntax highlighting when typesetting from xml (asciidoc/docbook)
Date: Mon, 30 Nov 2020 21:08:03 +0100	[thread overview]
Message-ID: <CALBOmsZWmA86OTcYeWJWOhWYawoQVkR48=8M87gxFz7-hOtCsw@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1314 bytes --]

Hi,

A while ago Hans provided an example of typesetting a document from a
docbook source.

I'm attaching an over-simplified version of the initial module (a
somewhat more complete version exists, but it still needs lots of
work) and a minimum working example with XML.

I'm not sure how to extend the xml parser to support typesetting from
something like this:

<section xml:id="_hello_world_in_c_and_context">
<title>Hello World in C and ConTeXt</title>
<programlisting language="cpp" linenumbering="unnumbered">#include
&lt;stdio.h&gt;

int main()
{
    printf("Hello, World!\n");
    return 0;
}</programlisting>
<programlisting language="context" linenumbering="numbered">\starttext
Hello world!
\stoptext</programlisting>
</section>

Maybe using the vim module would be the right approach here (since the
built-in parser only has support for a limited set of languages), but
I'm not exactly sure about the implementation to achieve that goal.

I started with

\startxmlsetups xml:programlisting
    \dontleavehmode
    \startframedtext[background=color,backgroundcolor=lightgray]
    \obeylines
    \tt
    \xmlflush{#1}
    \stopframedtext
\stopxmlsetups

but something more is needed to properly handle new lines and to
properly pass the text to vim, for example.

Any hints welcome.

Thank you,
    Mojca

[-- Attachment #2: test-syntax-highlighting.xml --]
[-- Type: text/xml, Size: 644 bytes --]

<?xml version="1.0" encoding="UTF-8"?>
<?asciidoc-toc?>
<?asciidoc-numbered?>
<article xmlns="http://docbook.org/ns/docbook" xmlns:xl="http://www.w3.org/1999/xlink" version="5.0" xml:lang="en">
<info>
<title>Syntax highlighting with AsciiDoc</title>
<date>2020-11-30</date>
</info>
<section xml:id="_hello_world_in_c">
<title>Hello World in C</title>
<programlisting language="cpp" linenumbering="unnumbered">#include &lt;stdio.h&gt;

int main()
{
    printf("Hello, World!\n");
    return 0;
}</programlisting>
<programlisting language="context" linenumbering="numbered">\starttext
Hello world!
\stoptext</programlisting>
</section>
</article>

[-- Attachment #3: test-syntax-highlighting.tex --]
[-- Type: application/octet-stream, Size: 108 bytes --]

\usemodule[asciidoc-basic]

\starttext
\xmlprocessfile{main}{test-syntax-highlighting.xml}{}
\stoptext

[-- Attachment #4: m-asciidoc-basic.tex --]
[-- Type: application/octet-stream, Size: 2324 bytes --]

% \enabletrackers[context*]

\usemodule[cals]

\startxmlsetups xml:whatever:a
    \xmlsetsetup {#1} {
        article|
        section|
        simpara|
        itemizedlist|orderedlist|listitem|
        emphasis|guibutton|
        literal|programlisting|
        superscript|subscript|
        equation|inlineequation|
        figure|mediaobject|informalfigure
    } {xml:*}
    \xmlsetsetup {#1} {
        imageobject/imagedata
    } {xml:image}
    \xmlsetsetup {#1} {
        imageobject
    } {+}
    \xmlsetfunction {#1} {table} {moduledata.cals.table}
\stopxmlsetups

\xmlregisterdocumentsetup{main}{xml:whatever:a}

\startxmlsetups xml:article
    \startbodymatter
        \xmlfilter{#1}{/section/command(xml:section)}
    \stopbodymatter
\stopxmlsetups

\startxmlsetups xml:section
    \startsectionlevel[title={\xmltext{#1}{/title}}]
        \xmlall{#1}{/!title}
        %\xmlall{#1}{}
    \stopsectionlevel
\stopxmlsetups

\startxmlsetups xml:simpara
    \xmlflush{#1}\par
\stopxmlsetups

\startxmlsetups xml:itemizedlist
    \startitemize
        \xmlflush{#1}
    \stopitemize
\stopxmlsetups

\defineitemgroup[ordered]
\setupitemgroup[ordered][1][symbol=n]
\setupitemgroup[ordered][2][symbol=a]
\setupitemgroup[ordered][3][symbol=i]

\startxmlsetups xml:orderedlist
    \startitemgroup[ordered]
        \xmlflush{#1}
    \stopitemgroup
\stopxmlsetups

\startxmlsetups xml:listitem
    \startitem
        \xmlflush{#1}
    \stopitem
\stopxmlsetups

\xmlmapvalue{emphasis}{strong}{\bf}
\xmlmapvalue{emphasis}{normal}{\em}

\startxmlsetups xml:emphasis
    \dontleavehmode
    \start
    \xmlvalue{emphasis}{\xmlattdef{#1}{role}{normal}}{normal}
    \xmlflush{#1}
    \stop
\stopxmlsetups

\startxmlsetups xml:literal
    \dontleavehmode
    \start
    \tt
    \xmlflush{#1}
    \stop
\stopxmlsetups

\startxmlsetups xml:programlisting
    \dontleavehmode
    \startframedtext[background=color,backgroundcolor=lightgray]
    \obeylines
    \tt
    \xmlflush{#1}
    \stopframedtext
\stopxmlsetups

\startluacode
    function xml.finalizers.tex.cdatatocontext(c)
        context(xml.cdata(c[1]))
    end
\stopluacode

\setupbodyfont
    [dejavu,10pt]

\setuplayout
    [middle]

\setupwhitespace
    [big]

[-- Attachment #5: test-syntax-highlighting.adoc --]
[-- Type: application/octet-stream, Size: 296 bytes --]

= Syntax highlighting with AsciiDoc
:source-highlighter: pygments

== Hello World in C and ConTeXt

[source,cpp]
----
#include <stdio.h>

int main()
{
    printf("Hello, World!\n");
    return 0;
}
----

[source,context,linenums]
----
\starttext
Hello world!
\stoptext
----

[-- Attachment #6: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

             reply	other threads:[~2020-11-30 20:08 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-30 20:08 Mojca Miklavec [this message]
2020-12-01 10:41 ` Hans Hagen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CALBOmsZWmA86OTcYeWJWOhWYawoQVkR48=8M87gxFz7-hOtCsw@mail.gmail.com' \
    --to=mojca.miklavec.lists@gmail.com \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).