3

Background

Using pandoc to convert a Markdown document into ConTeX and would like to frame a section.

Problem

Using \beforesection and \aftersection with \starttextbackground and its stopper only works with start/stop sections, which is unsupported by pandoc.

Code

Here is example code produced by pandoc:

\starttext
    \chapter{Chapter}
    \section{Section 1}
    \subsection{Summary}
    \input tufte
    \section{Section 2}
    \subsection{Summary}
    \input knuth
\stoptext

Question

Without using \startsection and \stopsection, how would you put a frame around an entire section?

Dave Jarvis
  • 11,809

2 Answers2

6

You can use a pandoc filter to create \startsection and \stopsection. Filters are described in the pandoc manual.

I modified the theorem.py sample filter to create a section for bibliographies as follows. Perhaps it will serve as a first step to solving your problem.

#!/usr/bin/env python

"""
Pandoc filter to convert divs with class="refs" to ConTeXt
\startBibliography-\stopBibliography. Based on theorem.py
"""

from pandocfilters import toJSONFilter, RawBlock, Div

def context(x):
    return RawBlock('context', x)

def reference_block(key, value, format, meta):
    if key == 'Div':
        [[ident, classes, kvs], contents] = value
        if ident == "refs":
            if format == "context":
            return([context('\\startBibliography[reference=refs,title=Sources]')] + contents + [context('\\stopBibliography')])

if __name__ == "__main__":
    toJSONFilter(reference_block)
2

Personally, I would favour the pandoc solution proposed in the other answer. It is also possible to implement something (fragile) on the macro level. Whenever a new section or higher level sectioning command begins, check whether we are in a section. You also need to check on \stoptext (unfortunately, you cannot put it in \everystoptext. This leads to weird behaviour.)

\definetextbackground
  [SectionFrame]
  [background=,
   framecolor=red,
   corner=round,
   location=paragraph,
   before={\blank[2*big]}]

\newconditional\insection
\setfalse\insection

\define\checksection{%
  \ifconditional\insection
    \stoptextbackground
  \fi
  \setfalse\insection
}

\setuphead
  [section]
  [before={%
      \checksection
      \starttextbackground[SectionFrame]
      \settrue\insection
    }]

\setuphead
  [subsection]
  [before=]

\setuphead
  [chapter]
  [before=\checksection]

\define\stoptext{%
  \checksection
  \csname clf_stoptext\endcsname
}

\starttext
\chapter{Chapter}
\section{Section 1}
\subsection{Summary}
\input tufte
\section{Section 2}
\subsection{Summary}
\input knuth
\stoptext
Henri Menke
  • 109,596