public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
* A writer for PreTeXt
@ 2019-04-05 15:01 Oscar Levin
       [not found] ` <9b82655a-3067-4696-8123-5b0b99496a70-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  0 siblings, 1 reply; 10+ messages in thread
From: Oscar Levin @ 2019-04-05 15:01 UTC (permalink / raw)
  To: pandoc-discuss


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

I am working on a custom (lua) writer for PreTeXt (https://pretextbook.org/), 
which I use to write open math textbooks.  In terms of syntax, PreTeXt is 
xml and  is very similar to docbook, so a lot of what I need was easily 
modified from the sample.lau custom writer.  

What I haven't been able to figure out is how to wrap sections in <section> 
tags.  That is, in the built in docbook writer, if you have a header, I 
think it creates a section with a title around the appropriate content.  Is 
there any way to do this with a custom lua writer?  

Perhaps a better approach would be to contribute a new built in writer by 
modifying the docbook writer.  I think there would be a good number of 
people who would find this writer useful.  Would such a contribution be 
considered?

Thanks,
Oscar.

-- 
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 post to this group, send email to pandoc-discuss-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/9b82655a-3067-4696-8123-5b0b99496a70%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

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

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

* Re: A writer for PreTeXt
       [not found] ` <9b82655a-3067-4696-8123-5b0b99496a70-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2019-04-05 17:27   ` John MacFarlane
       [not found]     ` <yh480ko95k4b2w.fsf-pgq/RBwaQ+zq8tPRBa0AtqxOck334EZe@public.gmane.org>
  0 siblings, 1 reply; 10+ messages in thread
From: John MacFarlane @ 2019-04-05 17:27 UTC (permalink / raw)
  To: Oscar Levin, pandoc-discuss


To group content into sections, you should be able to
use the function hierarchicalize from pandoc.utils:

https://pandoc.org/lua-filters.html#utils-hierarchicalize

This wraps the function that the Haskell writers use.

A regular writer might be considered, but I'd like to
know more about PreTeXt.  What is the advantage over
using, say, plain DocBook?  How many users are there?

Oscar Levin <oscarlevin-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:

> I am working on a custom (lua) writer for PreTeXt (https://pretextbook.org/), 
> which I use to write open math textbooks.  In terms of syntax, PreTeXt is 
> xml and  is very similar to docbook, so a lot of what I need was easily 
> modified from the sample.lau custom writer.  
>
> What I haven't been able to figure out is how to wrap sections in <section> 
> tags.  That is, in the built in docbook writer, if you have a header, I 
> think it creates a section with a title around the appropriate content.  Is 
> there any way to do this with a custom lua writer?  
>
> Perhaps a better approach would be to contribute a new built in writer by 
> modifying the docbook writer.  I think there would be a good number of 
> people who would find this writer useful.  Would such a contribution be 
> considered?
>
> Thanks,
> Oscar.
>
> -- 
> 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 post to this group, send email to pandoc-discuss-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/9b82655a-3067-4696-8123-5b0b99496a70%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.


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

* Re: A writer for PreTeXt
       [not found]     ` <yh480ko95k4b2w.fsf-pgq/RBwaQ+zq8tPRBa0AtqxOck334EZe@public.gmane.org>
@ 2019-04-11  4:43       ` Oscar Levin
       [not found]         ` <4b209782-0203-41f8-be61-707985d10afa-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  0 siblings, 1 reply; 10+ messages in thread
From: Oscar Levin @ 2019-04-11  4:43 UTC (permalink / raw)
  To: pandoc-discuss


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

Hi John,

Thanks for the hint.  I've been trying to wrap my head around all this 
(filters and their relation to custom writers, plus the logical structure 
of how the writer "walks" the AST, if that is even the right way of 
thinking about it), so far with little luck.  Maybe I'm stuck in the 
mindset of how xslt processes xml.  I'll keep hacking away.

I found an old custom JATS writer, but that seems abandoned (since there is 
regular writer for it now): there sections were handled by putting 
</section><section> where headers go, and then removing the first 
</section> and adding one at the end of the document.  That approach would 
not work for me, since I would like <subsection>.  I assume the 
hierarchicalize would allow for this?  So far I've been unable to figure it 
out though.

While I keep thinking about this, here is a little more about PreTeXt.  I'm 
not the author of it, but as I see it, the big advantage over docbook is 
math support (it was previously called MathBook XML).  I like it for my 
textbooks because it allows for theorems/proofs, and exercises/solutions, 
and so on.  The HTML output you get (via xslt) can put solutions or proofs 
in "knowls": a link you click on that expands a box with the content.  
Cross references also work this way (if you reference an earlier theorem, 
clicking on the link opens a box with the statement of the theorem, for 
example).  You can also process the xml into LaTeX for pdf output.

In terms of users, I can only speculate on lower bounds: the "gallery" of 
textbook projects on the PreTeXt website lists 33 completed books, and I 
know of at least a dozen more books currently under development.  The 
github repository (https://github.com/rbeezer/mathbook) has been forked 82 
times.  I'm not sure what you would consider a reasonable number of users; 
I would like to see the number of users grow, and I see having an easy way 
to get people started by converting a latex or word document into PreTeXt 
as a good step in that direction.

Thanks,
Oscar.

On Friday, April 5, 2019 at 11:27:19 AM UTC-6, John MacFarlane wrote:
>
>
> To group content into sections, you should be able to 
> use the function hierarchicalize from pandoc.utils: 
>
> https://pandoc.org/lua-filters.html#utils-hierarchicalize 
>
> This wraps the function that the Haskell writers use. 
>
> A regular writer might be considered, but I'd like to 
> know more about PreTeXt.  What is the advantage over 
> using, say, plain DocBook?  How many users are there? 
>
> Oscar Levin writes: 
>
> > I am working on a custom (lua) writer for PreTeXt (
> https://pretextbook.org/), 
> > which I use to write open math textbooks.  In terms of syntax, PreTeXt 
> is 
> > xml and  is very similar to docbook, so a lot of what I need was easily 
> > modified from the sample.lau custom writer.   
> > 
> > What I haven't been able to figure out is how to wrap sections in 
> <section> 
> > tags.  That is, in the built in docbook writer, if you have a header, I 
> > think it creates a section with a title around the appropriate content. 
>  Is 
> > there any way to do this with a custom lua writer?   
> > 
> > Perhaps a better approach would be to contribute a new built in writer 
> by 
> > modifying the docbook writer.  I think there would be a good number of 
> > people who would find this writer useful.  Would such a contribution be 
> > considered? 
> > 
> > Thanks, 
> > Oscar. 
> > 
>

-- 
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 post to this group, send email to pandoc-discuss-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/4b209782-0203-41f8-be61-707985d10afa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

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

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

* Re: A writer for PreTeXt
       [not found]         ` <4b209782-0203-41f8-be61-707985d10afa-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2019-04-11 10:48           ` BP Jonsson
       [not found]             ` <008eebaf-2dd4-32c6-9d68-63fc99fa1e27-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
  0 siblings, 1 reply; 10+ messages in thread
From: BP Jonsson @ 2019-04-11 10:48 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw, Oscar Levin

You need to familiarize yourself with how different elements are 
represnted in the Pandoc AST.  In my experience the best way to do 
that is not to read the documentation (althou8gh you want to do 
that too of course[^1]) but to type examples of the kinds of 
structures you want to work with in Markdown and study what they 
look like in native and json output.  A JSON prettyprinter which 
adds spacing and indentation is a very good help.  Start with 
simple things like `[text]{#id .class1 .class2 attr1=val1 
attr2=val2}` and go in to more complicated/nested structures once 
you understand them.

To help me with visualizing the Lua objects I use the table 
dumping function[^2] from the Penlight libraries[^3] enhanced by 
the following custom function which makes a copy of (the 
non-metatable parts of) the lua objects, adding a key for the 
element type which otherwise is buried in the metatables:

````lua
local pl_pretty = require "pl.pretty"

-- visualize a (tree of) Lua Pandoc elements
-- without destroying the originals
function showit (x)
     if 'table' == type(x) then
         local y = {} -- the "copy"
         for k, v in pairs(x) do
             y[k] = showit(v) -- expand recursively
         end
         -- show the tag from the metatable
         if y._t then
             -- if y._t already exists (it shouldn't) don't clobber it
             y._t = { _t = y._t, t = x.t }
         else
             -- otherwise just assign the value of x.t from the 
metatable to it
             y._t = x.t
         end
         return y
     else
         return x
     end
end

function dumpit (x)
     pl_pretty.dump(showit(x))
end
````

I have these living in my `~/.pandoc/init.lua` so that they are 
always available.  Saying `dumpit(element)` will then pretty-print 
the element structure to stderr.

[^1]: Above all the Lua filter documentation
     <http://pandoc.org/lua-filters.html#lua-type-reference>
     but also the element type definitions (which were all we had 
before the advent of lua filters.
 
<http://hackage.haskell.org/package/pandoc-types-1.17.5.4/docs/Text-Pandoc-Definition.html#t:Pandoc> 

     (may not be the latest version).

[^2]: 
<http://stevedonovan.github.io/Penlight/api/libraries/pl.pretty.html>

[^3]: <https://github.com/stevedonovan/Penlight>

Den 2019-04-11 kl. 06:43, skrev Oscar Levin:
> Hi John,
> 
> Thanks for the hint.  I've been trying to wrap my head around all this
> (filters and their relation to custom writers, plus the logical structure
> of how the writer "walks" the AST, if that is even the right way of
> thinking about it), so far with little luck.  Maybe I'm stuck in the
> mindset of how xslt processes xml.  I'll keep hacking away.
> 
> I found an old custom JATS writer, but that seems abandoned (since there is
> regular writer for it now): there sections were handled by putting
> </section><section> where headers go, and then removing the first
> </section> and adding one at the end of the document.  That approach would
> not work for me, since I would like <subsection>.  I assume the
> hierarchicalize would allow for this?  So far I've been unable to figure it
> out though.
> 
> While I keep thinking about this, here is a little more about PreTeXt.  I'm
> not the author of it, but as I see it, the big advantage over docbook is
> math support (it was previously called MathBook XML).  I like it for my
> textbooks because it allows for theorems/proofs, and exercises/solutions,
> and so on.  The HTML output you get (via xslt) can put solutions or proofs
> in "knowls": a link you click on that expands a box with the content.
> Cross references also work this way (if you reference an earlier theorem,
> clicking on the link opens a box with the statement of the theorem, for
> example).  You can also process the xml into LaTeX for pdf output.
> 
> In terms of users, I can only speculate on lower bounds: the "gallery" of
> textbook projects on the PreTeXt website lists 33 completed books, and I
> know of at least a dozen more books currently under development.  The
> github repository (https://github.com/rbeezer/mathbook) has been forked 82
> times.  I'm not sure what you would consider a reasonable number of users;
> I would like to see the number of users grow, and I see having an easy way
> to get people started by converting a latex or word document into PreTeXt
> as a good step in that direction.
> 
> Thanks,
> Oscar.
> 
> On Friday, April 5, 2019 at 11:27:19 AM UTC-6, John MacFarlane wrote:
>>
>>
>> To group content into sections, you should be able to
>> use the function hierarchicalize from pandoc.utils:
>>
>> https://pandoc.org/lua-filters.html#utils-hierarchicalize
>>
>> This wraps the function that the Haskell writers use.
>>
>> A regular writer might be considered, but I'd like to
>> know more about PreTeXt.  What is the advantage over
>> using, say, plain DocBook?  How many users are there?
>>
>> Oscar Levin writes:
>>
>>> I am working on a custom (lua) writer for PreTeXt (
>> https://pretextbook.org/),
>>> which I use to write open math textbooks.  In terms of syntax, PreTeXt
>> is
>>> xml and  is very similar to docbook, so a lot of what I need was easily
>>> modified from the sample.lau custom writer.
>>>
>>> What I haven't been able to figure out is how to wrap sections in
>> <section>
>>> tags.  That is, in the built in docbook writer, if you have a header, I
>>> think it creates a section with a title around the appropriate content.
>>   Is
>>> there any way to do this with a custom lua writer?
>>>
>>> Perhaps a better approach would be to contribute a new built in writer
>> by
>>> modifying the docbook writer.  I think there would be a good number of
>>> people who would find this writer useful.  Would such a contribution be
>>> considered?
>>>
>>> Thanks,
>>> Oscar.
>>>
>>
> 


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

* Re: A writer for PreTeXt
       [not found]             ` <008eebaf-2dd4-32c6-9d68-63fc99fa1e27-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
@ 2019-04-11 13:12               ` Oscar Levin
       [not found]                 ` <c945d963-d067-4fe0-804d-280dbbed9b91-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  0 siblings, 1 reply; 10+ messages in thread
From: Oscar Levin @ 2019-04-11 13:12 UTC (permalink / raw)
  To: pandoc-discuss


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

Thanks.  This sounds like it will be a big help.  I'll give it a shot.

Oscar.

On Thursday, April 11, 2019 at 4:48:23 AM UTC-6, BP Jonsson wrote:
>
> You need to familiarize yourself with how different elements are 
> represnted in the Pandoc AST.  In my experience the best way to do 
> that is not to read the documentation (althou8gh you want to do 
> that too of course[^1]) but to type examples of the kinds of 
> structures you want to work with in Markdown and study what they 
> look like in native and json output.  A JSON prettyprinter which 
> adds spacing and indentation is a very good help.  Start with 
> simple things like `[text]{#id .class1 .class2 attr1=val1 
> attr2=val2}` and go in to more complicated/nested structures once 
> you understand them. 
>
> To help me with visualizing the Lua objects I use the table 
> dumping function[^2] from the Penlight libraries[^3] enhanced by 
> the following custom function which makes a copy of (the 
> non-metatable parts of) the lua objects, adding a key for the 
> element type which otherwise is buried in the metatables: 
>
> ````lua 
> local pl_pretty = require "pl.pretty" 
>
> -- visualize a (tree of) Lua Pandoc elements 
> -- without destroying the originals 
> function showit (x) 
>      if 'table' == type(x) then 
>          local y = {} -- the "copy" 
>          for k, v in pairs(x) do 
>              y[k] = showit(v) -- expand recursively 
>          end 
>          -- show the tag from the metatable 
>          if y._t then 
>              -- if y._t already exists (it shouldn't) don't clobber it 
>              y._t = { _t = y._t, t = x.t } 
>          else 
>              -- otherwise just assign the value of x.t from the 
> metatable to it 
>              y._t = x.t 
>          end 
>          return y 
>      else 
>          return x 
>      end 
> end 
>
> function dumpit (x) 
>      pl_pretty.dump(showit(x)) 
> end 
> ```` 
>
> I have these living in my `~/.pandoc/init.lua` so that they are 
> always available.  Saying `dumpit(element)` will then pretty-print 
> the element structure to stderr. 
>
> [^1]: Above all the Lua filter documentation 
>      <http://pandoc.org/lua-filters.html#lua-type-reference> 
>      but also the element type definitions (which were all we had 
> before the advent of lua filters. 
>   
> <
> http://hackage.haskell.org/package/pandoc-types-1.17.5.4/docs/Text-Pandoc-Definition.html#t:Pandoc> 
>
>
>      (may not be the latest version). 
>
> [^2]: 
> <http://stevedonovan.github.io/Penlight/api/libraries/pl.pretty.html> 
>
> [^3]: <https://github.com/stevedonovan/Penlight> 
>
> Den 2019-04-11 kl. 06:43, skrev Oscar Levin: 
> > Hi John, 
> > 
> > Thanks for the hint.  I've been trying to wrap my head around all this 
> > (filters and their relation to custom writers, plus the logical 
> structure 
> > of how the writer "walks" the AST, if that is even the right way of 
> > thinking about it), so far with little luck.  Maybe I'm stuck in the 
> > mindset of how xslt processes xml.  I'll keep hacking away. 
> > 
> > I found an old custom JATS writer, but that seems abandoned (since there 
> is 
> > regular writer for it now): there sections were handled by putting 
> > </section><section> where headers go, and then removing the first 
> > </section> and adding one at the end of the document.  That approach 
> would 
> > not work for me, since I would like <subsection>.  I assume the 
> > hierarchicalize would allow for this?  So far I've been unable to figure 
> it 
> > out though. 
> > 
> > While I keep thinking about this, here is a little more about PreTeXt. 
>  I'm 
> > not the author of it, but as I see it, the big advantage over docbook is 
> > math support (it was previously called MathBook XML).  I like it for my 
> > textbooks because it allows for theorems/proofs, and 
> exercises/solutions, 
> > and so on.  The HTML output you get (via xslt) can put solutions or 
> proofs 
> > in "knowls": a link you click on that expands a box with the content. 
> > Cross references also work this way (if you reference an earlier 
> theorem, 
> > clicking on the link opens a box with the statement of the theorem, for 
> > example).  You can also process the xml into LaTeX for pdf output. 
> > 
> > In terms of users, I can only speculate on lower bounds: the "gallery" 
> of 
> > textbook projects on the PreTeXt website lists 33 completed books, and I 
> > know of at least a dozen more books currently under development.  The 
> > github repository (https://github.com/rbeezer/mathbook) has been forked 
> 82 
> > times.  I'm not sure what you would consider a reasonable number of 
> users; 
> > I would like to see the number of users grow, and I see having an easy 
> way 
> > to get people started by converting a latex or word document into 
> PreTeXt 
> > as a good step in that direction. 
> > 
> > Thanks, 
> > Oscar. 
> > 
> > On Friday, April 5, 2019 at 11:27:19 AM UTC-6, John MacFarlane wrote: 
> >> 
> >> 
> >> To group content into sections, you should be able to 
> >> use the function hierarchicalize from pandoc.utils: 
> >> 
> >> https://pandoc.org/lua-filters.html#utils-hierarchicalize 
> >> 
> >> This wraps the function that the Haskell writers use. 
> >> 
> >> A regular writer might be considered, but I'd like to 
> >> know more about PreTeXt.  What is the advantage over 
> >> using, say, plain DocBook?  How many users are there? 
> >> 
> >> Oscar Levin writes: 
> >> 
> >>> I am working on a custom (lua) writer for PreTeXt ( 
> >> https://pretextbook.org/), 
> >>> which I use to write open math textbooks.  In terms of syntax, PreTeXt 
> >> is 
> >>> xml and  is very similar to docbook, so a lot of what I need was 
> easily 
> >>> modified from the sample.lau custom writer. 
> >>> 
> >>> What I haven't been able to figure out is how to wrap sections in 
> >> <section> 
> >>> tags.  That is, in the built in docbook writer, if you have a header, 
> I 
> >>> think it creates a section with a title around the appropriate 
> content. 
> >>   Is 
> >>> there any way to do this with a custom lua writer? 
> >>> 
> >>> Perhaps a better approach would be to contribute a new built in writer 
> >> by 
> >>> modifying the docbook writer.  I think there would be a good number of 
> >>> people who would find this writer useful.  Would such a contribution 
> be 
> >>> considered? 
> >>> 
> >>> Thanks, 
> >>> Oscar. 
> >>> 
> >> 
> > 
>
>

-- 
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 post to this group, send email to pandoc-discuss-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/c945d963-d067-4fe0-804d-280dbbed9b91%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

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

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

* Re: A writer for PreTeXt
       [not found]                 ` <c945d963-d067-4fe0-804d-280dbbed9b91-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2019-04-13 15:05                   ` Oscar Levin
       [not found]                     ` <995bcf75-7470-4574-a44c-9cb7bfde3ce7-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  0 siblings, 1 reply; 10+ messages in thread
From: Oscar Levin @ 2019-04-13 15:05 UTC (permalink / raw)
  To: pandoc-discuss


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

Okay, I think I need another hint or two.  Looking around at other 
discussions here (specifically this one 
<https://groups.google.com/d/msg/pandoc-discuss/oFEcJTLDEqE/dzrxsv_XFwAJ>), 
I think the right approach is to roughly do the following: 

   1. Use utils.hierarchicalize to transform the list of all blocks of my 
   document into hierarchical elements.  
   2. Modify the elements (for each element at the first level, wrap it in 
   <section> ... </section>, for each element at the second level, wrap in 
   <subsection>...</subsection>).  
   3. Flatten the heirachical elements back into a list of blocks.  
   4. Let the custom writer continue to convert blocks and inlines as 
   normal.

So this leaves me with at least a few questions.

   1. Is this at all the correct way to think about this?
   2. The linked discussion suggests creating the hierarchical elements 
   inside a function "Pandoc(doc)".  I tried this and it looks like that never 
   gets called as part of the custom writer.  It looks like the Doc function 
   always gets called, but this is after all the blocks are converted.  So 
   where do I put the pre-processing procedure?  Do I need to have a separate 
   filter?
   3. Do you have any tips for how to modify the hierarchical elements?  
   4. I've seen the function Sec mentioned a number of places, but it 
   doesn't seem to do anything in the custom writer, at least not in the way 
   that I can create a function for Emph(s) and return "<em>" .. s .. "</em>", 
   for example.  

Thanks,
Oscar.

On Thursday, April 11, 2019 at 7:12:45 AM UTC-6, Oscar Levin wrote:
>
> Thanks.  This sounds like it will be a big help.  I'll give it a shot.
>
> Oscar.
>
> On Thursday, April 11, 2019 at 4:48:23 AM UTC-6, BP Jonsson wrote:
>>
>> You need to familiarize yourself with how different elements are 
>> represnted in the Pandoc AST.  In my experience the best way to do 
>> that is not to read the documentation (althou8gh you want to do 
>> that too of course[^1]) but to type examples of the kinds of 
>> structures you want to work with in Markdown and study what they 
>> look like in native and json output.  A JSON prettyprinter which 
>> adds spacing and indentation is a very good help.  Start with 
>> simple things like `[text]{#id .class1 .class2 attr1=val1 
>> attr2=val2}` and go in to more complicated/nested structures once 
>> you understand them. 
>>
>> To help me with visualizing the Lua objects I use the table 
>> dumping function[^2] from the Penlight libraries[^3] enhanced by 
>> the following custom function which makes a copy of (the 
>> non-metatable parts of) the lua objects, adding a key for the 
>> element type which otherwise is buried in the metatables: 
>>
>> ````lua 
>> local pl_pretty = require "pl.pretty" 
>>
>> -- visualize a (tree of) Lua Pandoc elements 
>> -- without destroying the originals 
>> function showit (x) 
>>      if 'table' == type(x) then 
>>          local y = {} -- the "copy" 
>>          for k, v in pairs(x) do 
>>              y[k] = showit(v) -- expand recursively 
>>          end 
>>          -- show the tag from the metatable 
>>          if y._t then 
>>              -- if y._t already exists (it shouldn't) don't clobber it 
>>              y._t = { _t = y._t, t = x.t } 
>>          else 
>>              -- otherwise just assign the value of x.t from the 
>> metatable to it 
>>              y._t = x.t 
>>          end 
>>          return y 
>>      else 
>>          return x 
>>      end 
>> end 
>>
>> function dumpit (x) 
>>      pl_pretty.dump(showit(x)) 
>> end 
>> ```` 
>>
>> I have these living in my `~/.pandoc/init.lua` so that they are 
>> always available.  Saying `dumpit(element)` will then pretty-print 
>> the element structure to stderr. 
>>
>> [^1]: Above all the Lua filter documentation 
>>      <http://pandoc.org/lua-filters.html#lua-type-reference> 
>>      but also the element type definitions (which were all we had 
>> before the advent of lua filters. 
>>   
>> <
>> http://hackage.haskell.org/package/pandoc-types-1.17.5.4/docs/Text-Pandoc-Definition.html#t:Pandoc> 
>>
>>
>>      (may not be the latest version). 
>>
>> [^2]: 
>> <http://stevedonovan.github.io/Penlight/api/libraries/pl.pretty.html> 
>>
>> [^3]: <https://github.com/stevedonovan/Penlight> 
>>
>> Den 2019-04-11 kl. 06:43, skrev Oscar Levin: 
>> > Hi John, 
>> > 
>> > Thanks for the hint.  I've been trying to wrap my head around all this 
>> > (filters and their relation to custom writers, plus the logical 
>> structure 
>> > of how the writer "walks" the AST, if that is even the right way of 
>> > thinking about it), so far with little luck.  Maybe I'm stuck in the 
>> > mindset of how xslt processes xml.  I'll keep hacking away. 
>> > 
>> > I found an old custom JATS writer, but that seems abandoned (since 
>> there is 
>> > regular writer for it now): there sections were handled by putting 
>> > </section><section> where headers go, and then removing the first 
>> > </section> and adding one at the end of the document.  That approach 
>> would 
>> > not work for me, since I would like <subsection>.  I assume the 
>> > hierarchicalize would allow for this?  So far I've been unable to 
>> figure it 
>> > out though. 
>> > 
>> > While I keep thinking about this, here is a little more about PreTeXt. 
>>  I'm 
>> > not the author of it, but as I see it, the big advantage over docbook 
>> is 
>> > math support (it was previously called MathBook XML).  I like it for my 
>> > textbooks because it allows for theorems/proofs, and 
>> exercises/solutions, 
>> > and so on.  The HTML output you get (via xslt) can put solutions or 
>> proofs 
>> > in "knowls": a link you click on that expands a box with the content. 
>> > Cross references also work this way (if you reference an earlier 
>> theorem, 
>> > clicking on the link opens a box with the statement of the theorem, for 
>> > example).  You can also process the xml into LaTeX for pdf output. 
>> > 
>> > In terms of users, I can only speculate on lower bounds: the "gallery" 
>> of 
>> > textbook projects on the PreTeXt website lists 33 completed books, and 
>> I 
>> > know of at least a dozen more books currently under development.  The 
>> > github repository (https://github.com/rbeezer/mathbook) has been 
>> forked 82 
>> > times.  I'm not sure what you would consider a reasonable number of 
>> users; 
>> > I would like to see the number of users grow, and I see having an easy 
>> way 
>> > to get people started by converting a latex or word document into 
>> PreTeXt 
>> > as a good step in that direction. 
>> > 
>> > Thanks, 
>> > Oscar. 
>> > 
>> > On Friday, April 5, 2019 at 11:27:19 AM UTC-6, John MacFarlane wrote: 
>> >> 
>> >> 
>> >> To group content into sections, you should be able to 
>> >> use the function hierarchicalize from pandoc.utils: 
>> >> 
>> >> https://pandoc.org/lua-filters.html#utils-hierarchicalize 
>> >> 
>> >> This wraps the function that the Haskell writers use. 
>> >> 
>> >> A regular writer might be considered, but I'd like to 
>> >> know more about PreTeXt.  What is the advantage over 
>> >> using, say, plain DocBook?  How many users are there? 
>> >> 
>> >> Oscar Levin writes: 
>> >> 
>> >>> I am working on a custom (lua) writer for PreTeXt ( 
>> >> https://pretextbook.org/), 
>> >>> which I use to write open math textbooks.  In terms of syntax, 
>> PreTeXt 
>> >> is 
>> >>> xml and  is very similar to docbook, so a lot of what I need was 
>> easily 
>> >>> modified from the sample.lau custom writer. 
>> >>> 
>> >>> What I haven't been able to figure out is how to wrap sections in 
>> >> <section> 
>> >>> tags.  That is, in the built in docbook writer, if you have a header, 
>> I 
>> >>> think it creates a section with a title around the appropriate 
>> content. 
>> >>   Is 
>> >>> there any way to do this with a custom lua writer? 
>> >>> 
>> >>> Perhaps a better approach would be to contribute a new built in 
>> writer 
>> >> by 
>> >>> modifying the docbook writer.  I think there would be a good number 
>> of 
>> >>> people who would find this writer useful.  Would such a contribution 
>> be 
>> >>> considered? 
>> >>> 
>> >>> Thanks, 
>> >>> Oscar. 
>> >>> 
>> >> 
>> > 
>>
>>

-- 
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 post to this group, send email to pandoc-discuss-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/995bcf75-7470-4574-a44c-9cb7bfde3ce7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

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

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

* Re: A writer for PreTeXt
       [not found]                     ` <995bcf75-7470-4574-a44c-9cb7bfde3ce7-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2019-04-14  0:16                       ` John MacFarlane
       [not found]                         ` <m2imvhwieq.fsf-pgq/RBwaQ+zq8tPRBa0AtqxOck334EZe@public.gmane.org>
  0 siblings, 1 reply; 10+ messages in thread
From: John MacFarlane @ 2019-04-14  0:16 UTC (permalink / raw)
  To: Oscar Levin, pandoc-discuss


I'm sorry, I might have led you down the wrong path.
hierarchicalize was really exposed for use in lua
filters, and the documentation is geared to that.
I thought it would be helpful in creating a custom
lua writer, but on second thought I'm not sure it is.

Here's another approach that would be more
straightforward.  Define a global variable
current_section_level in your lua filter.

When you encounter a level N header, you
check this variable.  If N <= current_section_level,
then you close open sections (by emiting
`</section>` and decrementing current_section_level
until current_section_level > N.  Then you
emit `<section>` plus `<hN>` and increment
current_section_level.

That's fairly sketchy, but it's the basic idea.
Main point is that you can keep state in global
variables in a lua custom writer.

Oscar Levin <oscarlevin-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:

> Okay, I think I need another hint or two.  Looking around at other 
> discussions here (specifically this one 
> <https://groups.google.com/d/msg/pandoc-discuss/oFEcJTLDEqE/dzrxsv_XFwAJ>), 
> I think the right approach is to roughly do the following: 
>
>    1. Use utils.hierarchicalize to transform the list of all blocks of my 
>    document into hierarchical elements.  
>    2. Modify the elements (for each element at the first level, wrap it in 
>    <section> ... </section>, for each element at the second level, wrap in 
>    <subsection>...</subsection>).  
>    3. Flatten the heirachical elements back into a list of blocks.  
>    4. Let the custom writer continue to convert blocks and inlines as 
>    normal.
>
> So this leaves me with at least a few questions.
>
>    1. Is this at all the correct way to think about this?
>    2. The linked discussion suggests creating the hierarchical elements 
>    inside a function "Pandoc(doc)".  I tried this and it looks like that never 
>    gets called as part of the custom writer.  It looks like the Doc function 
>    always gets called, but this is after all the blocks are converted.  So 
>    where do I put the pre-processing procedure?  Do I need to have a separate 
>    filter?
>    3. Do you have any tips for how to modify the hierarchical elements?  
>    4. I've seen the function Sec mentioned a number of places, but it 
>    doesn't seem to do anything in the custom writer, at least not in the way 
>    that I can create a function for Emph(s) and return "<em>" .. s .. "</em>", 
>    for example.  
>
> Thanks,
> Oscar.
>
> On Thursday, April 11, 2019 at 7:12:45 AM UTC-6, Oscar Levin wrote:
>>
>> Thanks.  This sounds like it will be a big help.  I'll give it a shot.
>>
>> Oscar.
>>
>> On Thursday, April 11, 2019 at 4:48:23 AM UTC-6, BP Jonsson wrote:
>>>
>>> You need to familiarize yourself with how different elements are 
>>> represnted in the Pandoc AST.  In my experience the best way to do 
>>> that is not to read the documentation (althou8gh you want to do 
>>> that too of course[^1]) but to type examples of the kinds of 
>>> structures you want to work with in Markdown and study what they 
>>> look like in native and json output.  A JSON prettyprinter which 
>>> adds spacing and indentation is a very good help.  Start with 
>>> simple things like `[text]{#id .class1 .class2 attr1=val1 
>>> attr2=val2}` and go in to more complicated/nested structures once 
>>> you understand them. 
>>>
>>> To help me with visualizing the Lua objects I use the table 
>>> dumping function[^2] from the Penlight libraries[^3] enhanced by 
>>> the following custom function which makes a copy of (the 
>>> non-metatable parts of) the lua objects, adding a key for the 
>>> element type which otherwise is buried in the metatables: 
>>>
>>> ````lua 
>>> local pl_pretty = require "pl.pretty" 
>>>
>>> -- visualize a (tree of) Lua Pandoc elements 
>>> -- without destroying the originals 
>>> function showit (x) 
>>>      if 'table' == type(x) then 
>>>          local y = {} -- the "copy" 
>>>          for k, v in pairs(x) do 
>>>              y[k] = showit(v) -- expand recursively 
>>>          end 
>>>          -- show the tag from the metatable 
>>>          if y._t then 
>>>              -- if y._t already exists (it shouldn't) don't clobber it 
>>>              y._t = { _t = y._t, t = x.t } 
>>>          else 
>>>              -- otherwise just assign the value of x.t from the 
>>> metatable to it 
>>>              y._t = x.t 
>>>          end 
>>>          return y 
>>>      else 
>>>          return x 
>>>      end 
>>> end 
>>>
>>> function dumpit (x) 
>>>      pl_pretty.dump(showit(x)) 
>>> end 
>>> ```` 
>>>
>>> I have these living in my `~/.pandoc/init.lua` so that they are 
>>> always available.  Saying `dumpit(element)` will then pretty-print 
>>> the element structure to stderr. 
>>>
>>> [^1]: Above all the Lua filter documentation 
>>>      <http://pandoc.org/lua-filters.html#lua-type-reference> 
>>>      but also the element type definitions (which were all we had 
>>> before the advent of lua filters. 
>>>   
>>> <
>>> http://hackage.haskell.org/package/pandoc-types-1.17.5.4/docs/Text-Pandoc-Definition.html#t:Pandoc> 
>>>
>>>
>>>      (may not be the latest version). 
>>>
>>> [^2]: 
>>> <http://stevedonovan.github.io/Penlight/api/libraries/pl.pretty.html> 
>>>
>>> [^3]: <https://github.com/stevedonovan/Penlight> 
>>>
>>> Den 2019-04-11 kl. 06:43, skrev Oscar Levin: 
>>> > Hi John, 
>>> > 
>>> > Thanks for the hint.  I've been trying to wrap my head around all this 
>>> > (filters and their relation to custom writers, plus the logical 
>>> structure 
>>> > of how the writer "walks" the AST, if that is even the right way of 
>>> > thinking about it), so far with little luck.  Maybe I'm stuck in the 
>>> > mindset of how xslt processes xml.  I'll keep hacking away. 
>>> > 
>>> > I found an old custom JATS writer, but that seems abandoned (since 
>>> there is 
>>> > regular writer for it now): there sections were handled by putting 
>>> > </section><section> where headers go, and then removing the first 
>>> > </section> and adding one at the end of the document.  That approach 
>>> would 
>>> > not work for me, since I would like <subsection>.  I assume the 
>>> > hierarchicalize would allow for this?  So far I've been unable to 
>>> figure it 
>>> > out though. 
>>> > 
>>> > While I keep thinking about this, here is a little more about PreTeXt. 
>>>  I'm 
>>> > not the author of it, but as I see it, the big advantage over docbook 
>>> is 
>>> > math support (it was previously called MathBook XML).  I like it for my 
>>> > textbooks because it allows for theorems/proofs, and 
>>> exercises/solutions, 
>>> > and so on.  The HTML output you get (via xslt) can put solutions or 
>>> proofs 
>>> > in "knowls": a link you click on that expands a box with the content. 
>>> > Cross references also work this way (if you reference an earlier 
>>> theorem, 
>>> > clicking on the link opens a box with the statement of the theorem, for 
>>> > example).  You can also process the xml into LaTeX for pdf output. 
>>> > 
>>> > In terms of users, I can only speculate on lower bounds: the "gallery" 
>>> of 
>>> > textbook projects on the PreTeXt website lists 33 completed books, and 
>>> I 
>>> > know of at least a dozen more books currently under development.  The 
>>> > github repository (https://github.com/rbeezer/mathbook) has been 
>>> forked 82 
>>> > times.  I'm not sure what you would consider a reasonable number of 
>>> users; 
>>> > I would like to see the number of users grow, and I see having an easy 
>>> way 
>>> > to get people started by converting a latex or word document into 
>>> PreTeXt 
>>> > as a good step in that direction. 
>>> > 
>>> > Thanks, 
>>> > Oscar. 
>>> > 
>>> > On Friday, April 5, 2019 at 11:27:19 AM UTC-6, John MacFarlane wrote: 
>>> >> 
>>> >> 
>>> >> To group content into sections, you should be able to 
>>> >> use the function hierarchicalize from pandoc.utils: 
>>> >> 
>>> >> https://pandoc.org/lua-filters.html#utils-hierarchicalize 
>>> >> 
>>> >> This wraps the function that the Haskell writers use. 
>>> >> 
>>> >> A regular writer might be considered, but I'd like to 
>>> >> know more about PreTeXt.  What is the advantage over 
>>> >> using, say, plain DocBook?  How many users are there? 
>>> >> 
>>> >> Oscar Levin writes: 
>>> >> 
>>> >>> I am working on a custom (lua) writer for PreTeXt ( 
>>> >> https://pretextbook.org/), 
>>> >>> which I use to write open math textbooks.  In terms of syntax, 
>>> PreTeXt 
>>> >> is 
>>> >>> xml and  is very similar to docbook, so a lot of what I need was 
>>> easily 
>>> >>> modified from the sample.lau custom writer. 
>>> >>> 
>>> >>> What I haven't been able to figure out is how to wrap sections in 
>>> >> <section> 
>>> >>> tags.  That is, in the built in docbook writer, if you have a header, 
>>> I 
>>> >>> think it creates a section with a title around the appropriate 
>>> content. 
>>> >>   Is 
>>> >>> there any way to do this with a custom lua writer? 
>>> >>> 
>>> >>> Perhaps a better approach would be to contribute a new built in 
>>> writer 
>>> >> by 
>>> >>> modifying the docbook writer.  I think there would be a good number 
>>> of 
>>> >>> people who would find this writer useful.  Would such a contribution 
>>> be 
>>> >>> considered? 
>>> >>> 
>>> >>> Thanks, 
>>> >>> Oscar. 
>>> >>> 
>>> >> 
>>> > 
>>>
>>>
>
> -- 
> 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 post to this group, send email to pandoc-discuss-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/995bcf75-7470-4574-a44c-9cb7bfde3ce7%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.


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

* Re: A writer for PreTeXt
       [not found]                         ` <m2imvhwieq.fsf-pgq/RBwaQ+zq8tPRBa0AtqxOck334EZe@public.gmane.org>
@ 2019-04-16  4:55                           ` Oscar Levin
       [not found]                             ` <f84d2d65-9da8-4d7c-81c2-3e00b8c24e66-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  0 siblings, 1 reply; 10+ messages in thread
From: Oscar Levin @ 2019-04-16  4:55 UTC (permalink / raw)
  To: pandoc-discuss


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

Brilliant!  I had hacked together another approach (essentially using 
regular expressions and substituting appropriately inside the Doc function) 
but it required passing over the document once for each section.  This will 
be much cleaner.

Bonus: you solved what was going to be my next question about indenting the 
xml to make it pretty (by keeping an indent global variable).

Thanks for your help.

Oscar.

On Saturday, April 13, 2019 at 6:16:30 PM UTC-6, John MacFarlane wrote:
>
>
> I'm sorry, I might have led you down the wrong path. 
> hierarchicalize was really exposed for use in lua 
> filters, and the documentation is geared to that. 
> I thought it would be helpful in creating a custom 
> lua writer, but on second thought I'm not sure it is. 
>
> Here's another approach that would be more 
> straightforward.  Define a global variable 
> current_section_level in your lua filter. 
>
> When you encounter a level N header, you 
> check this variable.  If N <= current_section_level, 
> then you close open sections (by emiting 
> `</section>` and decrementing current_section_level 
> until current_section_level > N.  Then you 
> emit `<section>` plus `<hN>` and increment 
> current_section_level. 
>
> That's fairly sketchy, but it's the basic idea. 
> Main point is that you can keep state in global 
> variables in a lua custom writer. 
>
> Oscar Levin <oscar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <javascript:>> writes: 
>
> > Okay, I think I need another hint or two.  Looking around at other 
> > discussions here (specifically this one 
> > <https://groups.google.com/d/msg/pandoc-discuss/oFEcJTLDEqE/dzrxsv_XFwAJ>), 
>
> > I think the right approach is to roughly do the following: 
> > 
> >    1. Use utils.hierarchicalize to transform the list of all blocks of 
> my 
> >    document into hierarchical elements.   
> >    2. Modify the elements (for each element at the first level, wrap it 
> in 
> >    <section> ... </section>, for each element at the second level, wrap 
> in 
> >    <subsection>...</subsection>).   
> >    3. Flatten the heirachical elements back into a list of blocks.   
> >    4. Let the custom writer continue to convert blocks and inlines as 
> >    normal. 
> > 
> > So this leaves me with at least a few questions. 
> > 
> >    1. Is this at all the correct way to think about this? 
> >    2. The linked discussion suggests creating the hierarchical elements 
> >    inside a function "Pandoc(doc)".  I tried this and it looks like that 
> never 
> >    gets called as part of the custom writer.  It looks like the Doc 
> function 
> >    always gets called, but this is after all the blocks are converted. 
>  So 
> >    where do I put the pre-processing procedure?  Do I need to have a 
> separate 
> >    filter? 
> >    3. Do you have any tips for how to modify the hierarchical elements? 
>   
> >    4. I've seen the function Sec mentioned a number of places, but it 
> >    doesn't seem to do anything in the custom writer, at least not in the 
> way 
> >    that I can create a function for Emph(s) and return "<em>" .. s .. 
> "</em>", 
> >    for example.   
> > 
> > Thanks, 
> > Oscar. 
> > 
> > On Thursday, April 11, 2019 at 7:12:45 AM UTC-6, Oscar Levin wrote: 
> >> 
> >> Thanks.  This sounds like it will be a big help.  I'll give it a shot. 
> >> 
> >> Oscar. 
> >> 
> >> On Thursday, April 11, 2019 at 4:48:23 AM UTC-6, BP Jonsson wrote: 
> >>> 
> >>> You need to familiarize yourself with how different elements are 
> >>> represnted in the Pandoc AST.  In my experience the best way to do 
> >>> that is not to read the documentation (althou8gh you want to do 
> >>> that too of course[^1]) but to type examples of the kinds of 
> >>> structures you want to work with in Markdown and study what they 
> >>> look like in native and json output.  A JSON prettyprinter which 
> >>> adds spacing and indentation is a very good help.  Start with 
> >>> simple things like `[text]{#id .class1 .class2 attr1=val1 
> >>> attr2=val2}` and go in to more complicated/nested structures once 
> >>> you understand them. 
> >>> 
> >>> To help me with visualizing the Lua objects I use the table 
> >>> dumping function[^2] from the Penlight libraries[^3] enhanced by 
> >>> the following custom function which makes a copy of (the 
> >>> non-metatable parts of) the lua objects, adding a key for the 
> >>> element type which otherwise is buried in the metatables: 
> >>> 
> >>> ````lua 
> >>> local pl_pretty = require "pl.pretty" 
> >>> 
> >>> -- visualize a (tree of) Lua Pandoc elements 
> >>> -- without destroying the originals 
> >>> function showit (x) 
> >>>      if 'table' == type(x) then 
> >>>          local y = {} -- the "copy" 
> >>>          for k, v in pairs(x) do 
> >>>              y[k] = showit(v) -- expand recursively 
> >>>          end 
> >>>          -- show the tag from the metatable 
> >>>          if y._t then 
> >>>              -- if y._t already exists (it shouldn't) don't clobber it 
> >>>              y._t = { _t = y._t, t = x.t } 
> >>>          else 
> >>>              -- otherwise just assign the value of x.t from the 
> >>> metatable to it 
> >>>              y._t = x.t 
> >>>          end 
> >>>          return y 
> >>>      else 
> >>>          return x 
> >>>      end 
> >>> end 
> >>> 
> >>> function dumpit (x) 
> >>>      pl_pretty.dump(showit(x)) 
> >>> end 
> >>> ```` 
> >>> 
> >>> I have these living in my `~/.pandoc/init.lua` so that they are 
> >>> always available.  Saying `dumpit(element)` will then pretty-print 
> >>> the element structure to stderr. 
> >>> 
> >>> [^1]: Above all the Lua filter documentation 
> >>>      <http://pandoc.org/lua-filters.html#lua-type-reference> 
> >>>      but also the element type definitions (which were all we had 
> >>> before the advent of lua filters. 
> >>>   
> >>> < 
> >>> 
> http://hackage.haskell.org/package/pandoc-types-1.17.5.4/docs/Text-Pandoc-Definition.html#t:Pandoc> 
>
> >>> 
> >>> 
> >>>      (may not be the latest version). 
> >>> 
> >>> [^2]: 
> >>> <http://stevedonovan.github.io/Penlight/api/libraries/pl.pretty.html> 
> >>> 
> >>> [^3]: <https://github.com/stevedonovan/Penlight> 
> >>> 
> >>> Den 2019-04-11 kl. 06:43, skrev Oscar Levin: 
> >>> > Hi John, 
> >>> > 
> >>> > Thanks for the hint.  I've been trying to wrap my head around all 
> this 
> >>> > (filters and their relation to custom writers, plus the logical 
> >>> structure 
> >>> > of how the writer "walks" the AST, if that is even the right way of 
> >>> > thinking about it), so far with little luck.  Maybe I'm stuck in the 
> >>> > mindset of how xslt processes xml.  I'll keep hacking away. 
> >>> > 
> >>> > I found an old custom JATS writer, but that seems abandoned (since 
> >>> there is 
> >>> > regular writer for it now): there sections were handled by putting 
> >>> > </section><section> where headers go, and then removing the first 
> >>> > </section> and adding one at the end of the document.  That approach 
> >>> would 
> >>> > not work for me, since I would like <subsection>.  I assume the 
> >>> > hierarchicalize would allow for this?  So far I've been unable to 
> >>> figure it 
> >>> > out though. 
> >>> > 
> >>> > While I keep thinking about this, here is a little more about 
> PreTeXt. 
> >>>  I'm 
> >>> > not the author of it, but as I see it, the big advantage over 
> docbook 
> >>> is 
> >>> > math support (it was previously called MathBook XML).  I like it for 
> my 
> >>> > textbooks because it allows for theorems/proofs, and 
> >>> exercises/solutions, 
> >>> > and so on.  The HTML output you get (via xslt) can put solutions or 
> >>> proofs 
> >>> > in "knowls": a link you click on that expands a box with the 
> content. 
> >>> > Cross references also work this way (if you reference an earlier 
> >>> theorem, 
> >>> > clicking on the link opens a box with the statement of the theorem, 
> for 
> >>> > example).  You can also process the xml into LaTeX for pdf output. 
> >>> > 
> >>> > In terms of users, I can only speculate on lower bounds: the 
> "gallery" 
> >>> of 
> >>> > textbook projects on the PreTeXt website lists 33 completed books, 
> and 
> >>> I 
> >>> > know of at least a dozen more books currently under development. 
>  The 
> >>> > github repository (https://github.com/rbeezer/mathbook) has been 
> >>> forked 82 
> >>> > times.  I'm not sure what you would consider a reasonable number of 
> >>> users; 
> >>> > I would like to see the number of users grow, and I see having an 
> easy 
> >>> way 
> >>> > to get people started by converting a latex or word document into 
> >>> PreTeXt 
> >>> > as a good step in that direction. 
> >>> > 
> >>> > Thanks, 
> >>> > Oscar. 
> >>> > 
> >>> > On Friday, April 5, 2019 at 11:27:19 AM UTC-6, John MacFarlane 
> wrote: 
> >>> >> 
> >>> >> 
> >>> >> To group content into sections, you should be able to 
> >>> >> use the function hierarchicalize from pandoc.utils: 
> >>> >> 
> >>> >> https://pandoc.org/lua-filters.html#utils-hierarchicalize 
> >>> >> 
> >>> >> This wraps the function that the Haskell writers use. 
> >>> >> 
> >>> >> A regular writer might be considered, but I'd like to 
> >>> >> know more about PreTeXt.  What is the advantage over 
> >>> >> using, say, plain DocBook?  How many users are there? 
> >>> >> 
> >>> >> Oscar Levin writes: 
> >>> >> 
> >>> >>> I am working on a custom (lua) writer for PreTeXt ( 
> >>> >> https://pretextbook.org/), 
> >>> >>> which I use to write open math textbooks.  In terms of syntax, 
> >>> PreTeXt 
> >>> >> is 
> >>> >>> xml and  is very similar to docbook, so a lot of what I need was 
> >>> easily 
> >>> >>> modified from the sample.lau custom writer. 
> >>> >>> 
> >>> >>> What I haven't been able to figure out is how to wrap sections in 
> >>> >> <section> 
> >>> >>> tags.  That is, in the built in docbook writer, if you have a 
> header, 
> >>> I 
> >>> >>> think it creates a section with a title around the appropriate 
> >>> content. 
> >>> >>   Is 
> >>> >>> there any way to do this with a custom lua writer? 
> >>> >>> 
> >>> >>> Perhaps a better approach would be to contribute a new built in 
> >>> writer 
> >>> >> by 
> >>> >>> modifying the docbook writer.  I think there would be a good 
> number 
> >>> of 
> >>> >>> people who would find this writer useful.  Would such a 
> contribution 
> >>> be 
> >>> >>> considered? 
> >>> >>> 
> >>> >>> Thanks, 
> >>> >>> Oscar. 
> >>> >>> 
> >>> >> 
> >>> > 
> >>> 
> >>> 
> > 
> > -- 
> > 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-...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <javascript:>. 
> > To post to this group, send email to pandoc-...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org 
> <javascript:>. 
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/pandoc-discuss/995bcf75-7470-4574-a44c-9cb7bfde3ce7%40googlegroups.com. 
>
> > For more options, visit https://groups.google.com/d/optout. 
>

-- 
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 post to this group, send email to pandoc-discuss-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/f84d2d65-9da8-4d7c-81c2-3e00b8c24e66%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

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

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

* Re: A writer for PreTeXt
       [not found]                             ` <f84d2d65-9da8-4d7c-81c2-3e00b8c24e66-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2019-04-16 16:24                               ` John MacFarlane
       [not found]                                 ` <yh480k36mh6hq2.fsf-pgq/RBwaQ+zq8tPRBa0AtqxOck334EZe@public.gmane.org>
  0 siblings, 1 reply; 10+ messages in thread
From: John MacFarlane @ 2019-04-16 16:24 UTC (permalink / raw)
  To: Oscar Levin, pandoc-discuss


Great! I hope you'll share your completed custom
writer here, and feel free to add a GitHub issue
requesting a normal pandoc writer for pretext.
(Having the custom writer will make this easier
if we decide to do it.)

Oscar Levin <oscarlevin-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:

> Brilliant!  I had hacked together another approach (essentially using 
> regular expressions and substituting appropriately inside the Doc function) 
> but it required passing over the document once for each section.  This will 
> be much cleaner.
>
> Bonus: you solved what was going to be my next question about indenting the 
> xml to make it pretty (by keeping an indent global variable).
>
> Thanks for your help.
>
> Oscar.
>
> On Saturday, April 13, 2019 at 6:16:30 PM UTC-6, John MacFarlane wrote:
>>
>>
>> I'm sorry, I might have led you down the wrong path. 
>> hierarchicalize was really exposed for use in lua 
>> filters, and the documentation is geared to that. 
>> I thought it would be helpful in creating a custom 
>> lua writer, but on second thought I'm not sure it is. 
>>
>> Here's another approach that would be more 
>> straightforward.  Define a global variable 
>> current_section_level in your lua filter. 
>>
>> When you encounter a level N header, you 
>> check this variable.  If N <= current_section_level, 
>> then you close open sections (by emiting 
>> `</section>` and decrementing current_section_level 
>> until current_section_level > N.  Then you 
>> emit `<section>` plus `<hN>` and increment 
>> current_section_level. 
>>
>> That's fairly sketchy, but it's the basic idea. 
>> Main point is that you can keep state in global 
>> variables in a lua custom writer. 
>>
>> Oscar Levin <oscar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <javascript:>> writes: 
>>
>> > Okay, I think I need another hint or two.  Looking around at other 
>> > discussions here (specifically this one 
>> > <https://groups.google.com/d/msg/pandoc-discuss/oFEcJTLDEqE/dzrxsv_XFwAJ>), 
>>
>> > I think the right approach is to roughly do the following: 
>> > 
>> >    1. Use utils.hierarchicalize to transform the list of all blocks of 
>> my 
>> >    document into hierarchical elements.   
>> >    2. Modify the elements (for each element at the first level, wrap it 
>> in 
>> >    <section> ... </section>, for each element at the second level, wrap 
>> in 
>> >    <subsection>...</subsection>).   
>> >    3. Flatten the heirachical elements back into a list of blocks.   
>> >    4. Let the custom writer continue to convert blocks and inlines as 
>> >    normal. 
>> > 
>> > So this leaves me with at least a few questions. 
>> > 
>> >    1. Is this at all the correct way to think about this? 
>> >    2. The linked discussion suggests creating the hierarchical elements 
>> >    inside a function "Pandoc(doc)".  I tried this and it looks like that 
>> never 
>> >    gets called as part of the custom writer.  It looks like the Doc 
>> function 
>> >    always gets called, but this is after all the blocks are converted. 
>>  So 
>> >    where do I put the pre-processing procedure?  Do I need to have a 
>> separate 
>> >    filter? 
>> >    3. Do you have any tips for how to modify the hierarchical elements? 
>>   
>> >    4. I've seen the function Sec mentioned a number of places, but it 
>> >    doesn't seem to do anything in the custom writer, at least not in the 
>> way 
>> >    that I can create a function for Emph(s) and return "<em>" .. s .. 
>> "</em>", 
>> >    for example.   
>> > 
>> > Thanks, 
>> > Oscar. 
>> > 
>> > On Thursday, April 11, 2019 at 7:12:45 AM UTC-6, Oscar Levin wrote: 
>> >> 
>> >> Thanks.  This sounds like it will be a big help.  I'll give it a shot. 
>> >> 
>> >> Oscar. 
>> >> 
>> >> On Thursday, April 11, 2019 at 4:48:23 AM UTC-6, BP Jonsson wrote: 
>> >>> 
>> >>> You need to familiarize yourself with how different elements are 
>> >>> represnted in the Pandoc AST.  In my experience the best way to do 
>> >>> that is not to read the documentation (althou8gh you want to do 
>> >>> that too of course[^1]) but to type examples of the kinds of 
>> >>> structures you want to work with in Markdown and study what they 
>> >>> look like in native and json output.  A JSON prettyprinter which 
>> >>> adds spacing and indentation is a very good help.  Start with 
>> >>> simple things like `[text]{#id .class1 .class2 attr1=val1 
>> >>> attr2=val2}` and go in to more complicated/nested structures once 
>> >>> you understand them. 
>> >>> 
>> >>> To help me with visualizing the Lua objects I use the table 
>> >>> dumping function[^2] from the Penlight libraries[^3] enhanced by 
>> >>> the following custom function which makes a copy of (the 
>> >>> non-metatable parts of) the lua objects, adding a key for the 
>> >>> element type which otherwise is buried in the metatables: 
>> >>> 
>> >>> ````lua 
>> >>> local pl_pretty = require "pl.pretty" 
>> >>> 
>> >>> -- visualize a (tree of) Lua Pandoc elements 
>> >>> -- without destroying the originals 
>> >>> function showit (x) 
>> >>>      if 'table' == type(x) then 
>> >>>          local y = {} -- the "copy" 
>> >>>          for k, v in pairs(x) do 
>> >>>              y[k] = showit(v) -- expand recursively 
>> >>>          end 
>> >>>          -- show the tag from the metatable 
>> >>>          if y._t then 
>> >>>              -- if y._t already exists (it shouldn't) don't clobber it 
>> >>>              y._t = { _t = y._t, t = x.t } 
>> >>>          else 
>> >>>              -- otherwise just assign the value of x.t from the 
>> >>> metatable to it 
>> >>>              y._t = x.t 
>> >>>          end 
>> >>>          return y 
>> >>>      else 
>> >>>          return x 
>> >>>      end 
>> >>> end 
>> >>> 
>> >>> function dumpit (x) 
>> >>>      pl_pretty.dump(showit(x)) 
>> >>> end 
>> >>> ```` 
>> >>> 
>> >>> I have these living in my `~/.pandoc/init.lua` so that they are 
>> >>> always available.  Saying `dumpit(element)` will then pretty-print 
>> >>> the element structure to stderr. 
>> >>> 
>> >>> [^1]: Above all the Lua filter documentation 
>> >>>      <http://pandoc.org/lua-filters.html#lua-type-reference> 
>> >>>      but also the element type definitions (which were all we had 
>> >>> before the advent of lua filters. 
>> >>>   
>> >>> < 
>> >>> 
>> http://hackage.haskell.org/package/pandoc-types-1.17.5.4/docs/Text-Pandoc-Definition.html#t:Pandoc> 
>>
>> >>> 
>> >>> 
>> >>>      (may not be the latest version). 
>> >>> 
>> >>> [^2]: 
>> >>> <http://stevedonovan.github.io/Penlight/api/libraries/pl.pretty.html> 
>> >>> 
>> >>> [^3]: <https://github.com/stevedonovan/Penlight> 
>> >>> 
>> >>> Den 2019-04-11 kl. 06:43, skrev Oscar Levin: 
>> >>> > Hi John, 
>> >>> > 
>> >>> > Thanks for the hint.  I've been trying to wrap my head around all 
>> this 
>> >>> > (filters and their relation to custom writers, plus the logical 
>> >>> structure 
>> >>> > of how the writer "walks" the AST, if that is even the right way of 
>> >>> > thinking about it), so far with little luck.  Maybe I'm stuck in the 
>> >>> > mindset of how xslt processes xml.  I'll keep hacking away. 
>> >>> > 
>> >>> > I found an old custom JATS writer, but that seems abandoned (since 
>> >>> there is 
>> >>> > regular writer for it now): there sections were handled by putting 
>> >>> > </section><section> where headers go, and then removing the first 
>> >>> > </section> and adding one at the end of the document.  That approach 
>> >>> would 
>> >>> > not work for me, since I would like <subsection>.  I assume the 
>> >>> > hierarchicalize would allow for this?  So far I've been unable to 
>> >>> figure it 
>> >>> > out though. 
>> >>> > 
>> >>> > While I keep thinking about this, here is a little more about 
>> PreTeXt. 
>> >>>  I'm 
>> >>> > not the author of it, but as I see it, the big advantage over 
>> docbook 
>> >>> is 
>> >>> > math support (it was previously called MathBook XML).  I like it for 
>> my 
>> >>> > textbooks because it allows for theorems/proofs, and 
>> >>> exercises/solutions, 
>> >>> > and so on.  The HTML output you get (via xslt) can put solutions or 
>> >>> proofs 
>> >>> > in "knowls": a link you click on that expands a box with the 
>> content. 
>> >>> > Cross references also work this way (if you reference an earlier 
>> >>> theorem, 
>> >>> > clicking on the link opens a box with the statement of the theorem, 
>> for 
>> >>> > example).  You can also process the xml into LaTeX for pdf output. 
>> >>> > 
>> >>> > In terms of users, I can only speculate on lower bounds: the 
>> "gallery" 
>> >>> of 
>> >>> > textbook projects on the PreTeXt website lists 33 completed books, 
>> and 
>> >>> I 
>> >>> > know of at least a dozen more books currently under development. 
>>  The 
>> >>> > github repository (https://github.com/rbeezer/mathbook) has been 
>> >>> forked 82 
>> >>> > times.  I'm not sure what you would consider a reasonable number of 
>> >>> users; 
>> >>> > I would like to see the number of users grow, and I see having an 
>> easy 
>> >>> way 
>> >>> > to get people started by converting a latex or word document into 
>> >>> PreTeXt 
>> >>> > as a good step in that direction. 
>> >>> > 
>> >>> > Thanks, 
>> >>> > Oscar. 
>> >>> > 
>> >>> > On Friday, April 5, 2019 at 11:27:19 AM UTC-6, John MacFarlane 
>> wrote: 
>> >>> >> 
>> >>> >> 
>> >>> >> To group content into sections, you should be able to 
>> >>> >> use the function hierarchicalize from pandoc.utils: 
>> >>> >> 
>> >>> >> https://pandoc.org/lua-filters.html#utils-hierarchicalize 
>> >>> >> 
>> >>> >> This wraps the function that the Haskell writers use. 
>> >>> >> 
>> >>> >> A regular writer might be considered, but I'd like to 
>> >>> >> know more about PreTeXt.  What is the advantage over 
>> >>> >> using, say, plain DocBook?  How many users are there? 
>> >>> >> 
>> >>> >> Oscar Levin writes: 
>> >>> >> 
>> >>> >>> I am working on a custom (lua) writer for PreTeXt ( 
>> >>> >> https://pretextbook.org/), 
>> >>> >>> which I use to write open math textbooks.  In terms of syntax, 
>> >>> PreTeXt 
>> >>> >> is 
>> >>> >>> xml and  is very similar to docbook, so a lot of what I need was 
>> >>> easily 
>> >>> >>> modified from the sample.lau custom writer. 
>> >>> >>> 
>> >>> >>> What I haven't been able to figure out is how to wrap sections in 
>> >>> >> <section> 
>> >>> >>> tags.  That is, in the built in docbook writer, if you have a 
>> header, 
>> >>> I 
>> >>> >>> think it creates a section with a title around the appropriate 
>> >>> content. 
>> >>> >>   Is 
>> >>> >>> there any way to do this with a custom lua writer? 
>> >>> >>> 
>> >>> >>> Perhaps a better approach would be to contribute a new built in 
>> >>> writer 
>> >>> >> by 
>> >>> >>> modifying the docbook writer.  I think there would be a good 
>> number 
>> >>> of 
>> >>> >>> people who would find this writer useful.  Would such a 
>> contribution 
>> >>> be 
>> >>> >>> considered? 
>> >>> >>> 
>> >>> >>> Thanks, 
>> >>> >>> Oscar. 
>> >>> >>> 
>> >>> >> 
>> >>> > 
>> >>> 
>> >>> 
>> > 
>> > -- 
>> > 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-...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <javascript:>. 
>> > To post to this group, send email to pandoc-...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org 
>> <javascript:>. 
>> > To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/pandoc-discuss/995bcf75-7470-4574-a44c-9cb7bfde3ce7%40googlegroups.com. 
>>
>> > For more options, visit https://groups.google.com/d/optout. 
>>
>
> -- 
> 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 post to this group, send email to pandoc-discuss-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/f84d2d65-9da8-4d7c-81c2-3e00b8c24e66%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.


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

* Re: A writer for PreTeXt
       [not found]                                 ` <yh480k36mh6hq2.fsf-pgq/RBwaQ+zq8tPRBa0AtqxOck334EZe@public.gmane.org>
@ 2019-05-27 20:02                                   ` Oscar Levin
  0 siblings, 0 replies; 10+ messages in thread
From: Oscar Levin @ 2019-05-27 20:02 UTC (permalink / raw)
  To: pandoc-discuss


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

After a long delay while I finished the semester, here is the mostly 
finished custom writer for PreTeXt.  You can also find it on github: 
https://github.com/oscarlevin/pandoc-pretext.

I'll make an issue for possible eventual native support.  In the mean time, 
thanks for all your help.

Oscar.

On Tuesday, April 16, 2019 at 10:24:53 AM UTC-6, John MacFarlane wrote:
>
>
> Great! I hope you'll share your completed custom 
> writer here, and feel free to add a GitHub issue 
> requesting a normal pandoc writer for pretext. 
> (Having the custom writer will make this easier 
> if we decide to do it.) 
>
>

-- 
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 post to this group, send email to pandoc-discuss-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/daf66a77-c843-4642-a702-d6939da65a9c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

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

[-- Attachment #2: pretext.lua --]
[-- Type: application/octet-stream, Size: 13231 bytes --]

-- This is a PreTeXt custom writer for pandoc,
-- based loosely on the JATS custom writter: https://github.com/mfenner/pandoc-jats. 
--
-- Invoke with: pandoc -t pretext.lua
--
-- Note:  you need not have lua installed on your system to use this
-- custom writer.  However, if you do have lua installed, you can
-- use it to test changes to the script.  'lua pretext.lua' will
-- produce informative error messages if your code contains
-- syntax errors.

-- The following breaks older pandoc installs, and it doesn't seem to be necessary for what I want to do.
-- local pipe = pandoc.pipe
-- local stringify = (require "pandoc.utils").stringify
-- local utils = require 'pandoc.utils'

-- The global variable PANDOC_DOCUMENT contains the full AST of
-- the document which is going to be written. It can be used to
-- configure the writer.
-- local meta = PANDOC_DOCUMENT.meta

-- global variable to keep track of indent level:
indents = 1

--We define the section names that correspond to the different levels.
sectionNames = {"section", "subsection", "subsubsection", "paragraphs", "paragraphs", "paragraphs"}
--sectionBuffer will be a stack that hold the current open divisions
sectionBuffer = {}

-- This function is called once for the whole document. Parameters:
-- body is a string, metadata is a table, variables is a table.
-- This gives you a fragment.  You could use the metadata table to
-- fill variables in a custom lua template.  Or, pass `--template=...`
-- to pandoc, and pandoc will add do the template processing as
-- usual.
function Doc(body, metadata, variables)

  -- close any open sections:
  while 1 <= #sectionBuffer do
    body = body .. "\n" .. string.rep("\t",#sectionBuffer) .. "</".. sectionBuffer[1]..">\n"
    table.remove(sectionBuffer,1)
  end
  -- add common start/end tags
  body = '<?xml version="1.0" encoding="UTF-8" ?>\n<!-- Generated by Pandoc using pretext.lua -->\n<pretext>\n<article>\n\n\n'..body..'\n\n\n</article>\n</pretext>'

  return body
end


-- Chose the image format based on the value of the
-- `image_format` meta value.
-- local image_format = meta.image_format
--   and stringify(meta.image_format)
--   or "png"
-- local image_mime_type = ({
--     jpeg = "image/jpeg",
--     jpg = "image/jpeg",
--     gif = "image/gif",
--     png = "image/png",
--     svg = "image/svg+xml",
--   })[image_format]
--   or error("unsupported image format `" .. img_format .. "`")
  
-- Character escaping
-- (might want to remove the quotes, double check pretext)
local function escape(s, in_attribute)
  return s:gsub("[<>&\"']",
    function(x)
      if x == '<' then
        return '&lt;'
      elseif x == '>' then
        return '&gt;'
      elseif x == '&' then
        return '&amp;'
      -- elseif x == '"' then
      --   return '&quot;'
      -- elseif x == "'" then
      --   return '&#39;'
      else
        return x
      end
    end)
end

-- Helper function to convert an attributes table into
-- a string that can be put into HTML tags.
local function attributes(attr)
  local attr_table = {}
  for x,y in pairs(attr) do
    if y and y ~= "" then
      if x == "id" then
        table.insert(attr_table, ' xml:id="' .. escape(y,true)..'"')
      else
        table.insert(attr_table, ' '..x .. '="' .. escape(y,true) .. '"')
      end
    end
  end
  return table.concat(attr_table)
end

-- Blocksep is used to separate block elements.
function Blocksep()
  return "\n\n"
end

-- The functions that follow render corresponding pandoc elements.
-- s is always a string, attr is always a table of attributes, and
-- items is always an array of strings (the items in a list).
-- Comments indicate the types of other variables.

function Str(s)
  return escape(s)
end

function Space()
  return " "
end

function SoftBreak()
  return " "
end

--No PreTeXt equivalent to linebreak.  Comment inserted for manual post-processing.
function LineBreak()
 return "<!-- linebreak -->"
end

function Emph(s)
  return "<em>" .. s .. "</em>"
end

-- No <bold> tag in PreTeXt, but <term> gives bold look.  Assume bold in source document denotes a term, otherwise author could search for <term> and fix case-by-case. 
function Strong(s)
  return "<term>" .. s .. "</term>"
end

function Subscript(s)
  return "<sub>" .. s .. "</sub>"
end

function Superscript(s)
  return "<sup>" .. s .. "</sup>"
end

-- No <smallcaps> in PreTeXt.  <alert> can be searched for and changed case-by-case.
function SmallCaps(s)
  return '<alert>' .. s .. '<alert>'
end

-- could also be "gone"
function Strikeout(s)
  return '<delete>' .. s .. '</delete>'
end

function Link(s, src, tit, attr)
  if string.sub(src, 1, 1) == "#" then
    return '<xref ref="'..escape(string.sub(src, 2))..'" />'
  else
    return '<url href="' .. escape(src,true) .. '">' .. s .. '</url>'
  end
end

-- Should this be enclosed in something like a stand-alone side-by-side?
function Image(s, src, tit, attr)
  return "<image source='" .. escape(src,true) .. "'/>"
end

function Code(s, attr)
  return "<c" .. attributes(attr) .. ">" .. escape(s) .. "</c>"
end

function InlineMath(s)
  return "<m>" .. escape(s) .. "</m>"
end

function DisplayMath(s)
  return "<me>" .. escape(s) .. "</me>"
end

function SingleQuoted(s)
  return "<sq>" .. s .. "</sq>"
end

function DoubleQuoted(s)
  return "<q>" .. s .. "</q>"
end

function Note(s)
  return "<fn>" .. s .. "</fn>"
end

function Span(s, attr)
 -- return "<span" .. attributes(attr) .. ">" .. s .. "</span>"
 return s
end

-- RowInline is a way to pass certain html or latex directly to the output if there is no equivalent in the AST.  Seems to only be for \cite, \ref. For now, we just leave it blank, so these elements are just dropped.
function RawInline(format, str)
  -- if format == "html" then
  --   return "<raw-html>"..str.."</raw-html>"
  -- else
  --   return "<raw "..format..">"..str.."</raw>"
  -- end
  return ''
end

-- FIXME: this might still be wrong.  Specifically, not sure what happens when multiple ids are present.
function Cite(s, cs)
  local ids = {}
  for _,cit in ipairs(cs) do
    table.insert(ids, cit.citationId)
  end
  return "<xref ref=\"" .. table.concat(ids, ",") ..
    "\">" .. s .. "</xref>"
end

function Plain(s)
  return s
end

function Para(s)
  -- here and below: tabs and tabsp(lus) are strings that add enough tab characters to make the output indented nicely.  Since "indents" changes each time these functions are called, these local variables need to be redefined each time.
  local tabs = string.rep("\t", indents)
  local tabsp = string.rep("\t", indents+1)
  return tabs.."<p>\n" .. tabsp .. s .. "\n".. tabs.."</p>"
end


function BlockQuote(s)
  local tabs = string.rep("\t", indents)
  local tabsp = string.rep("\t", indents+1)
  return tabs.."<blockquote>\n" ..tabsp.. s .. "\n"..tabs.."</blockquote>"
end

-- No <hrule> in PreTeXt.  Leave comment to be searched for.
function HorizontalRule()
--  return "<hr/>"
  return "<!-- Horizontal Rule Not Implimented -->"
end

-- Not sure what this does, so leaving as divs for now, until I see it show up.
function LineBlock(ls)
  return '<div style="white-space: pre-line;">' .. table.concat(ls, '\n') ..
         '</div>'
end

function CodeBlock(s, attr)
  local tabs = string.rep("\t", indents)
  -- -- If code block has class 'dot', pipe the contents through dot
  -- -- and base64, and include the base64-encoded png as a data: URL.
  -- if attr.class and string.match(' ' .. attr.class .. ' ',' dot ') then
  --   local img = pipe("base64", {}, pipe("dot", {"-T" .. image_format}, s))
  --   return '<img src="data:' .. image_mime_type .. ';base64,' .. img .. '"/>'
  -- -- otherwise treat as code (one could pipe through a highlighter)
  -- else
    return tabs.."<pre>" .. escape(s) ..
           "</pre>"
  -- end
end

function BulletList(items)
  local tabs = string.rep("\t", indents)
  local tabsp = string.rep("\t", indents+1)
  local buffer = {}
  for _, item in pairs(items) do
    table.insert(buffer, tabsp.."<li>\n"..tabsp .. item .. "\n"..tabsp.."</li>\n")
  end
  return tabs.."<p><ul>\n" .. table.concat(buffer, "\n") .. "\n"..tabs.."</ul></p>"
end

function OrderedList(items)
  local tabs = string.rep("\t", indents)
  local tabsp = string.rep("\t", indents+1)
  local buffer = {}
  for _, item in pairs(items) do
    table.insert(buffer, tabsp.."<li>\n" .. tabsp  .. item .. "\n".. tabsp.."</li>\n")
  end
  return tabs.."<p><ol>\n"..table.concat(buffer, "\n").."\n"..tabs.."</ol></p>"
end

function DefinitionList(items)
  local tabs = string.rep("\t", indents)
  local tabsp = string.rep("\t", indents+1)
  local tabspp = string.rep("\t", indents+2)
  local buffer = {}
  for _,item in pairs(items) do
    local k, v = next(item)
    table.insert(buffer, tabsp.."<dt>" .. k .. "</dt>\n"..tabspp.."<dd>" ..
                   table.concat(v, "</dd>\n<dd>") .. "</dd>")
  end
  return tabs.."<dl>\n" .. table.concat(buffer, "\n") .. "\n"..tabs.."</dl>"
end

-- PreTeXt does not have anything like this, but leaving it in to avoid errors.  Author can search and address case-by-case.
-- Convert pandoc alignment to something HTML can use.
-- align is AlignLeft, AlignRight, AlignCenter, or AlignDefault.
function html_align(align)
  if align == 'AlignLeft' then
    return 'left'
  elseif align == 'AlignRight' then
    return 'right'
  elseif align == 'AlignCenter' then
    return 'center'
  else
    return 'left'
  end
end

function CaptionedImage(src, tit, caption, attr)
  local tabs = string.rep("\t", indents)
  local tabsp = string.rep("\t", indents+1)
   return tabs..'<figure>\n\t<image source="' .. escape(src,true) ..
      '"/>\n' ..
      tabsp..'<caption>' .. caption .. '</caption>\n</figure>'
end

-- Caption is a string, aligns is an array of strings,
-- widths is an array of floats, headers is an array of
-- strings, rows is an array of arrays of strings.
function Table(caption, aligns, widths, headers, rows)
  local tabs = string.rep("\t", indents)
  local tabsp = string.rep("\t", indents+1)
  local tabspp = string.rep("\t", indents+2)
  local buffer = {}
  local function add(s)
    table.insert(buffer, s)
  end
  add(tabs.."<table>")
  -- if caption ~= "" then -- tabules need captions always
    add(tabsp.."<caption>" .. caption .. "</caption>")
  -- end
  if widths and widths[1] ~= 0 then
    for _, w in pairs(widths) do
      add('<col width="' .. string.format("%.0f%%", w * 100) .. '" />')
    end
  end
  add(tabsp..'<tabular>')
  local header_row = {}
  local empty_header = true
  for i, h in pairs(headers) do
    local align = html_align(aligns[i])
    table.insert(header_row, tabspp..'<cell halign="' .. align .. '">' .. h .. '</cell>')
    empty_header = empty_header and h == ""
  end
  if empty_header then
    head = ""
  else
    add(tabsp..'<row header="yes">')
    for _,h in pairs(header_row) do
      add(h)
    end
    add(tabsp..'</row>')
  end
  local class = "even"
  for _, row in pairs(rows) do
    class = (class == "even" and "odd") or "even"
    add(tabsp..'<row class="' .. class .. '">')
    for i,c in pairs(row) do
      add(tabspp..'<cell halign="' .. html_align(aligns[i]) .. '">' .. c .. '</cell>')
    end
    add(tabsp..'</row>')
  end
  add(tabsp..'</tabular>\n'..tabs..'</table>')
  return table.concat(buffer,'\n')
end

function RawBlock(format, str)
  return "<cd>\n" .. str .. "\n</cd>"
end

-- We use "sectionBuffer" to keep track of open division names, and close them when headers of not-higher levels are reached.  
-- Note this puts the close division tags after <divs>, if those were implimented.
-- lev is an integer, the header level.
function Header(lev, s, attr)
  -- buffer holds closing tags.
  local buffer = ""
  -- if the current level is less than the current number of nestings, close it up.
  while lev <= #sectionBuffer do
    buffer = buffer .. string.rep("\t",#sectionBuffer) .. "</".. sectionBuffer[1]..">\n"
    table.remove(sectionBuffer,1)
  end
  -- add the current division to the stack.
  table.insert(sectionBuffer,1,sectionNames[lev])
  -- Find numbers of tabs:
  indents = #sectionBuffer + 1
  local tabs = string.rep("\t", indents-1)
  local tabsp = string.rep("\t", indents)
  -- return closing division tags, starting division tag and title:
  return buffer .. "\n" .. tabs .. "<"..sectionNames[lev]..attributes(attr)..">\n" .. tabsp.."<title>"..s.."</title>"
end

-- Divs only seem to show up with specific markdown (or maybe converting from HTML).  The issue is that opening div's show up before new headers, so the close division tags and open div tags are in the wrong order.  Eventually, this could be switched in post processing (Doc function).
function Div(s, attr)
  -- return "<div" .. attributes(attr) .. ">\n" .. s .. "</div>"
  return '<!-- div attr='..attributes(attr).. '-->\n'..s..'<!--</div attr='.. attributes(attr)..'>-->'
end


-- The following code will produce runtime warnings when you haven't defined
-- all of the functions you need for the custom writer, so it's useful
-- to include when you're working on a writer.
local meta = {}
meta.__index =
  function(_, key)
    io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key))
    return function() return "" end
  end
setmetatable(_G, meta)


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

end of thread, other threads:[~2019-05-27 20:02 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-05 15:01 A writer for PreTeXt Oscar Levin
     [not found] ` <9b82655a-3067-4696-8123-5b0b99496a70-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2019-04-05 17:27   ` John MacFarlane
     [not found]     ` <yh480ko95k4b2w.fsf-pgq/RBwaQ+zq8tPRBa0AtqxOck334EZe@public.gmane.org>
2019-04-11  4:43       ` Oscar Levin
     [not found]         ` <4b209782-0203-41f8-be61-707985d10afa-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2019-04-11 10:48           ` BP Jonsson
     [not found]             ` <008eebaf-2dd4-32c6-9d68-63fc99fa1e27-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2019-04-11 13:12               ` Oscar Levin
     [not found]                 ` <c945d963-d067-4fe0-804d-280dbbed9b91-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2019-04-13 15:05                   ` Oscar Levin
     [not found]                     ` <995bcf75-7470-4574-a44c-9cb7bfde3ce7-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2019-04-14  0:16                       ` John MacFarlane
     [not found]                         ` <m2imvhwieq.fsf-pgq/RBwaQ+zq8tPRBa0AtqxOck334EZe@public.gmane.org>
2019-04-16  4:55                           ` Oscar Levin
     [not found]                             ` <f84d2d65-9da8-4d7c-81c2-3e00b8c24e66-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2019-04-16 16:24                               ` John MacFarlane
     [not found]                                 ` <yh480k36mh6hq2.fsf-pgq/RBwaQ+zq8tPRBa0AtqxOck334EZe@public.gmane.org>
2019-05-27 20:02                                   ` Oscar Levin

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