public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
* Lua: print an AST value?
@ 2019-05-26 11:20 Axel Rauschmayer
       [not found] ` <29f97153-8af9-442b-8458-6bd5d219a4f8-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  0 siblings, 1 reply; 5+ messages in thread
From: Axel Rauschmayer @ 2019-05-26 11:20 UTC (permalink / raw)
  To: pandoc-discuss


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

As an error message in a Lua filter, I’d like to print an AST value in some 
manner:

   - I found plans for outputting AST values to Markdown etc. That would be 
   the best solution, but it’s not ready yet. 
   https://groups.google.com/d/topic/pandoc-discuss/LM1l4z8xEhQ/discussion
   - Alternatively, printing the AST would also do. E.g., it could look 
   like here: https://pandoc.org/try/

-- 
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/29f97153-8af9-442b-8458-6bd5d219a4f8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

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

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

* Re: Lua: print an AST value?
       [not found] ` <29f97153-8af9-442b-8458-6bd5d219a4f8-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2019-05-26 17:44   ` BPJ
  2019-05-26 21:13   ` Albert Krewinkel
  2019-05-28  9:31   ` BPJ
  2 siblings, 0 replies; 5+ messages in thread
From: BPJ @ 2019-05-26 17:44 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

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

Have you tried `pandoc.utils.stringify()`?

It doesn't include formatting but it's better than nothing. You could even
write a small `pandoc.walk_*()` filter which inserts some basic
"formatting" as literal strings like `*` around Emph and run it on an AST
value before stringifying:

````lua
local para = pandoc.Para({emph})
local insert_formatting = {
  Emph = function (elem)
    return {
      pandoc.Str('*'),
      elem,
      pandoc.Str('*'),
    }
  end
}
para = pandoc.walk_block(para, insert_formatting)
local str = pandoc.stringify(para)
````

I agree that it would be good to have an API function to convert an AST
element to markup, sort of `pandoc.read()` in reverse.

Den sön 26 maj 2019 13:21Axel Rauschmayer <rauschma-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> skrev:

> As an error message in a Lua filter, I’d like to print an AST value in
> some manner:
>
>    - I found plans for outputting AST values to Markdown etc. That would
>    be the best solution, but it’s not ready yet.
>    https://groups.google.com/d/topic/pandoc-discuss/LM1l4z8xEhQ/discussion
>    - Alternatively, printing the AST would also do. E.g., it could look
>    like here: https://pandoc.org/try/
>
> --
> 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/29f97153-8af9-442b-8458-6bd5d219a4f8%40googlegroups.com
> <https://groups.google.com/d/msgid/pandoc-discuss/29f97153-8af9-442b-8458-6bd5d219a4f8%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> 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/CADAJKhC5orrK2vS_ELxRJHgmD2UQk%2Btg-2AqamGMFzyKf74U5A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

[-- Attachment #2: Type: text/html, Size: 4239 bytes --]

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

* Re: Lua: print an AST value?
       [not found] ` <29f97153-8af9-442b-8458-6bd5d219a4f8-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  2019-05-26 17:44   ` BPJ
@ 2019-05-26 21:13   ` Albert Krewinkel
       [not found]     ` <875zpwq5pt.fsf-9EawChwDxG8hFhg+JK9F0w@public.gmane.org>
  2019-05-28  9:31   ` BPJ
  2 siblings, 1 reply; 5+ messages in thread
From: Albert Krewinkel @ 2019-05-26 21:13 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

Axel Rauschmayer writes:

> As an error message in a Lua filter, I’d like to print an AST value in
> some manner:

This is not really supported yet. The closest you can get right now is
by monkey-patching Lua types, similar to what I proposed here:
https://github.com/jgm/pandoc/issues/5092#issuecomment-444526879

Just printing the native Haskell AST representation would be easy[1],
but I wonder if a more Lua-oriented output would be more appropriate.

[1]: https://github.com/tarleb/pandoc/commit/b66ae48913b3440c65d5cc3252d76d9b18a0ed9a

-- 
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/875zpwq5pt.fsf%40zeitkraut.de.
For more options, visit https://groups.google.com/d/optout.


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

* Re: Lua: print an AST value?
       [not found]     ` <875zpwq5pt.fsf-9EawChwDxG8hFhg+JK9F0w@public.gmane.org>
@ 2019-05-27 18:54       ` John MacFarlane
  0 siblings, 0 replies; 5+ messages in thread
From: John MacFarlane @ 2019-05-27 18:54 UTC (permalink / raw)
  To: Albert Krewinkel, pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw


Can't you use a generic lua table printer, like

https://github.com/kikito/inspect.lua


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

* Re: Lua: print an AST value?
       [not found] ` <29f97153-8af9-442b-8458-6bd5d219a4f8-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  2019-05-26 17:44   ` BPJ
  2019-05-26 21:13   ` Albert Krewinkel
@ 2019-05-28  9:31   ` BPJ
  2 siblings, 0 replies; 5+ messages in thread
From: BPJ @ 2019-05-28  9:31 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

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

@Axel I misunderstood what you wanted to do. I use the `pl.pretty`[^1]
library from penlight[^2] to dump AST structures, however `element.t` isn't
automatically included since it is defined only in the metatable, and
sometimes `pl.pretty` will get into an infinite loop because of the
metatables. To work around these issues I use a function which copies the
element table structure to an ordinary table structure without a metatable,
including copying `element.t` to `copy._t` as needed — prepending more
underscores until a unique key is obtained —, and pass the output of that
function to `pl.pretty.dump()`:

````lua
local function copy_elem (elem)
  if 'table' ~= type(elem) then return elem end
  local copy = {}
  for k,v in pairs(elem) do
    copy[k] = copy_elem(v) -- yes call recursively!
  end
  if nil ~= elem.t and rawget(elem, 't') ~= elem.t then
    local k = '_t'
    while nil ~= elem[k] do
      k = '_' .. k
    end
    copy[k] = elem.t
  end
  return copy
end

-- elsewhere:

pretty_dump = require"pl.pretty".dump

pretty_dump(copy_elem(elem))
````

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


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


Den sön 26 maj 2019 13:21Axel Rauschmayer <rauschma-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> skrev:

> As an error message in a Lua filter, I’d like to print an AST value in
> some manner:
>
>    - I found plans for outputting AST values to Markdown etc. That would
>    be the best solution, but it’s not ready yet.
>    https://groups.google.com/d/topic/pandoc-discuss/LM1l4z8xEhQ/discussion
>    - Alternatively, printing the AST would also do. E.g., it could look
>    like here: https://pandoc.org/try/
>
> --
> 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/29f97153-8af9-442b-8458-6bd5d219a4f8%40googlegroups.com
> <https://groups.google.com/d/msgid/pandoc-discuss/29f97153-8af9-442b-8458-6bd5d219a4f8%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> 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/CADAJKhDhB6R4_zse%3Drs-ubN%3DqJChqyt-Gr8pa2FGETYi9YQeOw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

[-- Attachment #2: Type: text/html, Size: 5150 bytes --]

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

end of thread, other threads:[~2019-05-28  9:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-26 11:20 Lua: print an AST value? Axel Rauschmayer
     [not found] ` <29f97153-8af9-442b-8458-6bd5d219a4f8-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2019-05-26 17:44   ` BPJ
2019-05-26 21:13   ` Albert Krewinkel
     [not found]     ` <875zpwq5pt.fsf-9EawChwDxG8hFhg+JK9F0w@public.gmane.org>
2019-05-27 18:54       ` John MacFarlane
2019-05-28  9:31   ` BPJ

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