ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* How to do code listings & sections from XML (AsciiDoc)
@ 2022-08-12 15:37 Mojca Miklavec via ntg-context
  2022-08-12 21:20 ` Aditya Mahajan via ntg-context
  2022-08-12 22:30 ` Max Chernoff via ntg-context
  0 siblings, 2 replies; 5+ messages in thread
From: Mojca Miklavec via ntg-context @ 2022-08-12 15:37 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Mojca Miklavec

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

Hi,

Hans helped me out with some asciidoc processing a while ago.

I played a bit further, but I'm currently facing two minor issues:

(a) I don't know how to turn program listing (verbatim code) from xml
to (perhaps, vim-based) syntax highlighting (but most importantly, to
make sure that line breaks work properly)

(b) some section titles are missing


Ad (a). The source that looks similar to this:

<programlisting language="cpp" linenumbering="unnumbered">
int main()
{
    return 0;
}
</programlisting>

should be converted into some kind of equivalent of

% on top
\usemodule[vim]
\definevimtyping [CPP] [syntax=cpp]

\startCPP
int main()
{
    return 0;
}
\stopCPP

In the attachment I have the source document (test.adoc), the
generated docbook xml that should be further processed, and test.tex
that one processes to get the final pdf.

Any hints would be greatly appreciated.

Mojca

[-- Attachment #2: test.tex --]
[-- Type: application/octet-stream, Size: 109 bytes --]

\usemodule[asciidoc-manual]

\starttext

%\showlayout

\xmlprocessfile{main}{test.xml}{}

\stoptext

[-- Attachment #3: test.adoc --]
[-- Type: application/octet-stream, Size: 1693 bytes --]

:toclevels: 4
:toc: left
:source-highlighter: pygments
:icons: font
:stem:
:experimental:

= Document title (completely missing)

== This title seems to be missing

Hello code ...

=== How it works

[source,cpp]
----
// test.cpp:

halfword tex_copy_node(halfword p)
{
    /*tex
        We really need a stub for copying because mem might move in the meantime due to resizing!
    */
    if (p < 0 || p >= lmt_node_memory_state.nodes_data.allocated) {
        return tex_formatted_error("nodes", "attempt to copy an impossible node %d", (int) p);
    } else if (p > lmt_node_memory_state.reserved && lmt_node_memory_state.nodesizes[p] == 0) {
        return tex_formatted_error("nodes", "attempt to copy a free %s node %d", get_node_name(node_type(p)), (int) p);
    } else {
        /*tex type of node */
        halfword t = node_type(p);
        int i = get_node_size(t);
        /*tex current node being fabricated for new list */
        halfword r = tex_get_node(i);
        /*tex this saves work */
        memcpy((void *) (lmt_node_memory_state.nodes + r), (void *) (lmt_node_memory_state.nodes + p), (sizeof(memoryword) * (unsigned) i));
        if (tex_nodetype_is_complex(i)) {
         // halfword copy_stub;
            if (tex_nodetype_has_attributes(t)) {
                add_attribute_reference(node_attr(p));
                node_prev(r) = null;
                lmt_properties_copy(lmt_lua_state.lua_instance, r, p);
            }
            node_next(r) = null;
            switch (t) {
                case glue_node:
                    copy_sub_list(glue_leader_ptr(r), glue_leader_ptr(p));
                    break;
----

[-- Attachment #4: test.xml --]
[-- Type: text/xml, Size: 2046 bytes --]

<?xml version="1.0" encoding="UTF-8"?>
<?asciidoc-toc maxdepth="4"?>
<?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>Document title (completely missing)</title>
<date>2022-08-12</date>
</info>
<section xml:id="_this_title_seems_to_be_missing">
<title>This title seems to be missing</title>
<simpara>Hello code &#8230;&#8203;</simpara>
<section xml:id="_how_it_works">
<title>How it works</title>
<programlisting language="cpp" linenumbering="unnumbered">// test.cpp:

halfword tex_copy_node(halfword p)
{
    /*tex
        We really need a stub for copying because mem might move in the meantime due to resizing!
    */
    if (p &lt; 0 || p &gt;= lmt_node_memory_state.nodes_data.allocated) {
        return tex_formatted_error("nodes", "attempt to copy an impossible node %d", (int) p);
    } else if (p &gt; lmt_node_memory_state.reserved &amp;&amp; lmt_node_memory_state.nodesizes[p] == 0) {
        return tex_formatted_error("nodes", "attempt to copy a free %s node %d", get_node_name(node_type(p)), (int) p);
    } else {
        /*tex type of node */
        halfword t = node_type(p);
        int i = get_node_size(t);
        /*tex current node being fabricated for new list */
        halfword r = tex_get_node(i);
        /*tex this saves work */
        memcpy((void *) (lmt_node_memory_state.nodes + r), (void *) (lmt_node_memory_state.nodes + p), (sizeof(memoryword) * (unsigned) i));
        if (tex_nodetype_is_complex(i)) {
         // halfword copy_stub;
            if (tex_nodetype_has_attributes(t)) {
                add_attribute_reference(node_attr(p));
                node_prev(r) = null;
                lmt_properties_copy(lmt_lua_state.lua_instance, r, p);
            }
            node_next(r) = null;
            switch (t) {
                case glue_node:
                    copy_sub_list(glue_leader_ptr(r), glue_leader_ptr(p));
                    break;</programlisting>
</section>
</section>
</article>

[-- Attachment #5: m-asciidoc-manual.tex --]
[-- Type: application/octet-stream, Size: 6848 bytes --]

% \enabletrackers[context*]

\usemodule[cals]

% \startxmlsetups xml:whatever:a
%     \xmlsetsetup {#1} {*} {xml:*}
% \stopxmlsetups

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

\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
    %\startstandardmakeup
    %    \setupalign[middle]
    %    \start
    %        \bfd \darkred \setupinterlinespace
    %        \begstrut
    %        \xmltext{#1}{/info/title}
    %        \endstrut
    %        \par
    %    \stop
    %    \blank[3*big]
    %    \xmldoiftext {#1} {/info/author/personname} {
    %        \start
    %            \bfa \setupinterlinespace
    %            \begstrut
    %            \xmltext{#1}{/info/author/personname/firstname}
    %            \space
    %            \xmltext{#1}{/info/author/personname/surname}
    %            \endstrut
    %            \par
    %        \stop
    %    }
    %    \blank[3*big]
    %    \xmldoiftext {#1} {/info/date} {
    %        \xmlfirst {#1} {/info/date}
    %    }
    %    \vfill
    %\stopstandardmakeup
    \startfrontmatter
        \starttitle[title={Table of Contents}]
            \placelist[section,subsection,subsubsection][criterium=text]
        \stoptitle
    \stopfrontmatter
    \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:guibutton
    \dontleavehmode
    \start
    \bf
    %\inframed[offset=0pt,frame=off,leftframe=on,rightframe=on]{\kern2pt\bf
    $[$\xmlflush{#1}$]$%\kern2pt}
    \stop
\stopxmlsetups

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

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

\startxmlsetups xml:superscript
    \high{\xmlflush{#1}}
\stopxmlsetups

\startxmlsetups xml:subscript
    \low{\xmlflush{#1}}
\stopxmlsetups

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

\startxmlsetups xml:equation
    \startformula
        \xmlfilter{#1}{/mathphrase/cdatatocontext()}
    \stopformula
\stopxmlsetups

\startxmlsetups xml:inlineequation
    \startimath
        \xmlfilter{#1}{/mathphrase/cdatatocontext()}
    \stopimath
\stopxmlsetups

\startxmlsetups xml:image
    \removeunwantedspaces
    \dontleavehmode
    \externalfigure[\xmlatt{#1}{fileref}]
    \ignorespaces
\stopxmlsetups

\startxmlsetups xml:figure
    \startplacefigure[title={\xmltext{#1}{/title}}]
        % depends ...
        \ignorespaces
        \midaligned{\xmlall{#1}{/!title}}
        \removeunwantedspaces
    \stopplacefigure
\stopxmlsetups

\startxmlsetups xml:informalfigure
    \startlinecorrection
        \ignorespaces
        \xmlall{#1}{/!title}
        \removeunwantedspaces
    \stoplinecorrection
\stopxmlsetups

\startxmlsetups xml:mediaobject
    % depends ...
    \ignorespaces
    \xmlall{#1}{/!textobject}
    \removeunwantedspaces
\stopxmlsetups


\definepapersize
    [menu]
    [width=19.6cm,
     height=29.4cm]
\setuppapersize
    [menu]

\setuplayout
    [topspace=2.5cm,
     header=25pt,
     headerdistance=\lineheight,
     footer=0pt,
     footerdistance=0pt,
     height=middle]

\setupcolors
    [state=start,cmyk=yes,rgb=no]
\definecolor
    [dsblack]
%   [c=0.277,m=0.254,y=0.250,k=0.281]
    [c=0.71,m=0.65,y=0.64,k=0.72]
%   [x=232323]
\definecolor
    [dsgraphite]
    [c=0.68,m=0.61,y=0.60,k=0.47]
\definecolor
    [dssilver]
    [c=0.36,m=0.29,y=0.3,k=0]
\definecolor
    [dsorange]
%   [c=0,m=0.309,y=0.359,k=0]
    [c=0,m=0.79,y=0.92,k=0]
%   [x=F15D2D]

\setupbodyfont
    [dejavu,10pt]

\setuplayout
    [middle]

\setupwhitespace
    [big]

\setuphead
    [chapter]
    [style=\bfd,
     color=dsblack,
     number=no,
     placehead=no,
     before=,
     after=,
     incrementnumber=no,
     numberwidth=0pt]
\setuphead
    [title]
    [placehead=yes]
\setuphead
    [section]
    [style=\bfb,
     color=dsblack]
\setuphead
    [subsection]
    [style=\bfa,
     sectionnumber=no,
     ownnumber=yes,
     color=dsorange]
\setuphead
    [subsubsection]
    [style=\bf,
     color=dsgraphite]

\setupinteraction
    [state=start]

\setuppagenumbering
    [location=]

\setupitemize
    [packed]

%\setupbackgrounds
%    [page]
%    [background=color,
%     backgroundcolor=dsblack]
\setupcolors
    [textcolor=dsblack]

\setupexternalfigures
    [maxwidth=\textwidth]

\setupheader
    [strut=yes,style=\tfx]

%\setupheader
%  [text]
%  [before={\startframed[frame=off,bottomframe=on,framecolor=red]},
%   after={\stopframed}]

\setupbackgrounds
    [header]
    [text]
    [bottomframe=on,rulethickness=1pt]

\setupheadertexts
    [{\tfx\setupinterlinespace
      \framed[width=.5\textwidth,align=flushleft,frame=off]
        {\color[dsorange]{\bfx\getmarking[chapter]}\\
        {\tfxx It could work like this: \getmarking[title]}\\
        \strut}}]
    [{\raise1.5\lineheight\hbox{\framed[offset=overlay,frame=off]{\externalfigure[cow][height=\lineheight]}}}]

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

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

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

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

end of thread, other threads:[~2022-08-13  6:15 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-12 15:37 How to do code listings & sections from XML (AsciiDoc) Mojca Miklavec via ntg-context
2022-08-12 21:20 ` Aditya Mahajan via ntg-context
2022-08-13  6:13   ` Mojca Miklavec via ntg-context
2022-08-13  6:15   ` Mojca Miklavec via ntg-context
2022-08-12 22:30 ` Max Chernoff via ntg-context

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