Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AsciiDoc Reader / AsciiDoc input support #1456

Open
ERnsTL opened this issue Jul 26, 2014 · 76 comments
Open

Add AsciiDoc Reader / AsciiDoc input support #1456

ERnsTL opened this issue Jul 26, 2014 · 76 comments

Comments

@ERnsTL
Copy link

ERnsTL commented Jul 26, 2014

Greetings,

I would like to hereby suggest the addition of AsciiDoc input resp. an AsciiDoc Reader.

Besides Markdown, this format is growing in popularity, also in use inside a publishing toolchain (asciidoc -> docbook -> pdf/epub/html). Currently the only other viable implementation is asciidoctor, which uses Ruby or JRuby, but it is AsciiDoc-only in its input format and not a universal markup converter, like pandoc.

I am aware of only one relevant discussion thread regarding this, which showed positive echo for this feature. Someone actually had some basic code there, but I am not sure if kuznero resp. Roman Kuznetsov still has his code from back then to start from, but anyway, it would certainly make sense to have AsciiDoc input in the feature set.

@mpickering
Copy link
Collaborator

I have started working on this, it might not be ready for a few months.

@mpickering
Copy link
Collaborator

Have you tried going AsciiDoc -> DocBook -> Pandoc? Can you describe the shortcomings of doing this if you have?

@ERnsTL
Copy link
Author

ERnsTL commented Aug 7, 2014

Thanks for your positive comment!

I personally have not tried the conversion chain as you mentioned. The current choices are bulky with regards to its dependencies, while not being and not aspiring to be universal markup translators, like pandoc. Going through intermediate formats instead of one pandoc invocation seems hacky to me.

Recently, I read about two publishing houses switching away from LaTeX and moving to AsciiDoc as their source format, so I gather that it fulfills the needs of technical writing well, also regarding referencing and I find it useful to have another capable plain-text format for documents, if software support is good and can easily convert between different formats and offers multiple output choices. Which is where this feature would come in ;-)

I personally would also like to write articles and possibly a book rather in AsciiDoc than LaTeX, which is - at least my personal - motivation for this feature.

@alexborisov
Copy link

👍 I would love to see support for reading asciidoc files in pandoc. It was actually my primary reason to use pandoc. At the moment i have a bit of a hacky solution converting my asciidoc into html and then feeding that into pandoc. I would love to eliminate the extra project dependency and simplify my build chain.

@ciampix
Copy link

ciampix commented Nov 19, 2014

I can testify that this would be a very useful addition to pandoc, thanks in advance mr. mpickering!

@jgm
Copy link
Owner

jgm commented Apr 20, 2015

PR #2100 contributes a basic AsciiDoc reader (with many features not yet implemented).
@mpickering, how far did you get on your AsciiDoc reader? Is it farther along than #2100, or not as far along? It would be good to put something in the repository (in a branch) for people to work on to advance the project.

@mpickering
Copy link
Collaborator

I commented on the #2100

@romario89
Copy link

I agree to that asciidoc is getting popular. I've just tried to convert the book Pro Git 2 into epub using pandoc and soon noticed that the book was coded in asciidoc, and pandoc was unable to read it.

@benhourigan
Copy link

I’ve tried an asciidoc > html (via https://github.com/asciidoctor/asciidoctor) > epub (via pandoc) conversion chain and it works extremely well except for the following issue.

Asciidoctor wraps all HTML elements in divs with additional classes. This stops pandoc from splitting the epub automatically at headings, because it will never see a 'naked' h1 etc.

Also mentioned this issue here: asciidoctor/asciidoctor#184

The ePub file I created using this method did not pass epubcheck 3.0.1 because it contained one duplicate ID (something I could have avoided). More seriously, the way footnotes are handled is not compliant, and raised several errors like this:

ERROR: …/EPUB.epub/ch003.xhtml(2918,1223): '_footnote_4': fragment identifier is not defined in 'ch003.xhtml'
ERROR: …/EPUB.epub/ch008.xhtml(2065,27): '_footnoteref_1': fragment identifier is not defined in 'ch008.xhtml'

(paths truncated with ellipsis at start)

At the risk of stating the obvious, it's important that pandoc-generated epubs from any source format avoid epubcheck validation errors, as authors and publishers may need to submit these epubs to storefronts that will require epubcheck compliance (i.e. Smashwords, iBooks). Many of the Github-hosted CLI epub generators I've tried (e.g. https://github.com/avdgaag/rpub) omit consideration of epubcheck compliance, so this may not be an obvious point after all. The most common point of failure seems to be the manifest, which pandoc does correctly, which is great. But it could be yet more robust, as the above errors indicate.

@jgm
Copy link
Owner

jgm commented Jul 5, 2015

+++ Ben Hourigan [Jul 05 15 08:56 ]:

I’ve tried an asciidoc > html (via
[1]https://github.com/asciidoctor/asciidoctor) > epub (via pandoc)
conversion chain and it works extremely well except for the following
issue.

Asciidoctor wraps all HTML elements in divs with additional classes.
This stops pandoc from splitting the epub automatically at headings,
because it will never see a 'naked' h1 etc.

Also discussed this issue here: [2]asciidoctor/asciidoctor#184

You could handle this easily with a filter that strips out the
outer Divs before the EPUB writer sees it.

The ePub file I created using this method did not pass epubcheck 3.0.1
because it contained one duplicate ID (something I could have avoided)
and, more seriously, the way footnotes are handled is not compliant,
and raised several errors like this:
ERROR: …/EPUB.epub/ch003.xhtml(2918,1223): '_footnote_4': fragment identifier is
not defined in 'ch003.xhtml'
ERROR: …/EPUB.epub/ch008.xhtml(2065,27): '_footnoteref_1': fragment identifier i
s not defined in 'ch008.xhtml'

When I convert pandoc's README to epub3, I see no errors
with epubcheck 3.0.1 (and README has several footnotes).

My guess is that the HTML footnotes produced by asciidoctor
are not read by pandoc as native pandoc footnotes, and that
is the underlying issue.

If you attach a short sample file (of HTML produced by
asciidoctor), we could confirm that.

Unfortunately, there's no standard way of doing footnotes
in HTML, so the HTML reader never produces a Note element.

@benhourigan
Copy link

Hope this is a sufficient sample:

<div class="paragraph">
<p>… the kind of politics that the liberal economist F. A. Hayek called &#8220;socialist.&#8221; <span class="footnote">[<a id="_footnoteref_1" class="footnote" href="#_footnote_1" title="View footnote.">1</a>]</span></p>
</div>

@jgm
Copy link
Owner

jgm commented Jul 5, 2015

Could you attach or link to the generated (noncompliant) epub itself?

@jgm
Copy link
Owner

jgm commented Jul 5, 2015

By the way, here's a simple filter (undiv.hs) that will remove your content divs. Run with --filter undiv.hs:

import Text.Pandoc.JSON

main = toJSONFilter undiv
  where undiv (Div (ident, ["content"], kvs) bs) = bs
        undiv b = [b]

@jgm
Copy link
Owner

jgm commented Jul 5, 2015

Depending on how asciidoc formats the notes, you may be able to get the HTML reader to parse them as notes. If you use -f html+epub_html_exts, then pandoc will interpret an element with the type attribute set to footnote or rearnote as a note, and an element with the type attribute set to noteref as a note reference, where the href attribute is an internal link to the corresponding footnote or rearnote. It looks as if asciidoc doesn't quite do it that way, but you could use a filter to add the needed type attributes, and then you'd be there.

@jgm
Copy link
Owner

jgm commented Jul 5, 2015

you could use a filter to add the needed type attributes, and then you'd be there.

Sorry, this is a bit misleading. Since a filter is applied only after the HTML reader, this wouldn't work unless you first filtered, then piped the resulting HTML into another invocation of pandoc. Anyway, there are numerous tools you could use to insert the type attribute where it's needed in the HTML, before passing to pandoc.

@jgm
Copy link
Owner

jgm commented Jul 5, 2015

Or maybe asciidoctor could be persuaded to insert the needed type attributes in the HTML.

@jgm
Copy link
Owner

jgm commented Jul 5, 2015

Actually, rather than the epub itself, it would be most useful for me to have the HTML from which it was generated.

@benhourigan
Copy link

Thanks for the filter. Will try this out. You can get the HTML file from https://www.dropbox.com/s/c2ror63pz16hc3w/2015-07-06-BH-STG-adoc-test.html?dl=0

@jgm
Copy link
Owner

jgm commented Jul 6, 2015

I tried:

% pandoc adoc-test.html -t epub3 -o adoc.epub
% epubcheck adoc.epub
Epubcheck Version 3.0.1

Validating against EPUB version 3.0
ERROR: adoc.epub: could not parse ch006.xhtml: duplicate id: cracks

Check finished with warnings or errors

So I edited adoc-test.html and changed one of the duplicate cracks ids to cracks2. I then regenerated the epub using pandoc and epubcheck gave no validation errors.
Are you using the latest version of pandoc?

@jgm
Copy link
Owner

jgm commented Jul 6, 2015

PS. You might have more success using asciidoc to produce DocBook, then converting that with pandoc. Have you tried that route?

@benhourigan
Copy link

Ah, damn---I didn't think to do a version check. Sorry for being such a novice. I'd been using 1.13.2, which is the latest version on homebrew. Will install 1.15 and try again. Your results sound promising.

@benhourigan
Copy link

BTW, as of pandoc 1.13.2, when I tried the asciidoctor docbook > pandoc epub route the output from docbook was inferior to the output from HTML. One particular thing that I noticed was that admonition blocks came in to the epub as blockquotes without an additional class, and so couldn't be styled specifically with CSS.

@jgm
Copy link
Owner

jgm commented Jul 6, 2015

Currently DocBook elements like <important>, <caution>,
<note>, <tip> ar rendered as a block quote starting with
a single paragraph with the word "Important", "Caution",
"Note", or "Tip" in strong. I'm not sure this is ideal;
we could switch to using divs. However, even with the
present setup, it would be simple to intercept these
block quotes in a filter and change them to divs. Just look
for

BlockQuote (Para [Strong [Str "Important"]] : xs)

and convert that to

Div ("", ["admonition"], []) xs

+++ Ben Hourigan [Jul 06 15 10:34 ]:

BTW, as of pandoc 1.13.2, when I tried the asciidoctor docbook > pandoc
epub route the output from docbook was inferior to the output from
HTML. One particular thing that I noticed was that admonition blocks
came in to the epub as blockquotes without an additional class, and so
couldn't be styled specifically with CSS.


Reply to this email directly or [1]view it on GitHub.

References

  1. Add AsciiDoc Reader / AsciiDoc input support #1456 (comment)

@benhourigan
Copy link

With the duplicate ID sorted in the .adoc file, from the pandoc 1.15 produces from the asciidoctor.html an epub that passes ePubcheck! :)

Inability to split the file at chapter headings remains an issue. Now trying the undiv filter. At first I got this error:

undiv.hs: createProcess: runInteractiveProcess: exec: does not exist (No such file or directory)

Then after installing ghc (7.8.4) and cabal-install (1.22.0.0) (not sure if latter was necessary) from homebrew, I got this error.

pandoc: Error running filter undiv.hs
fd:4: hPutBuf: resource vanished (Broken pipe)

Oddly, if I run chmod +x undiv.hs, I go back to getting the first error again.

All files used during generation here: https://www.dropbox.com/s/40uv3f4ad2fwuco/bh-undiv.hs-test.zip?dl=0 I'm running a script called generate.sh, which is just the following command:

pandoc -t epub --filter undiv.hs --epub-cover-image=cover.png --epub-stylesheet=epub.css --epub-metadata=metadata.xml --epub-chapter-level=1 -o EPUB.epub TEST-v32-Justin-Comments-In.html

@jgm
Copy link
Owner

jgm commented Jul 7, 2015

Is undiv.hs in your working directory?

What OS are you on?

+++ Ben Hourigan [Jul 06 15 18:13 ]:

With the duplicate ID sorted in the .adoc file, from the pandoc 1.15
produces from the asciidoctor.html an epub that passes ePubcheck! :)

Inability to split the file at chapter headings remains an issue. Now
trying the undiv filter. At first I got this error:
undiv.hs: createProcess: runInteractiveProcess: exec: does not exist (No such fi
le or directory)

Then after installing ghc (7.8.4) and cabal-install (1.22.0.0) (not
sure if latter was necessary) from homebrew, I got this error.
pandoc: Error running filter undiv.hs
fd:4: hPutBuf: resource vanished (Broken pipe)

Oddly, if I run chmod +x undiv.hs, I go back to getting the first error
again.

All files used during generation here:
[1]https://www.dropbox.com/s/40uv3f4ad2fwuco/bh-undiv.hs-test.zip?dl=0
I'm running a script called generate.sh, which is just the following
command:
pandoc -t epub --filter undiv.hs --epub-cover-image=cover.png --epub-stylesheet=
epub.css --epub-metadata=metadata.xml --epub-chapter-level=1 -o EPUB.epub TEST-v
32-Justin-Comments-In.html


Reply to this email directly or [2]view it on GitHub.

References

  1. https://www.dropbox.com/s/40uv3f4ad2fwuco/bh-undiv.hs-test.zip?dl=0
  2. Add AsciiDoc Reader / AsciiDoc input support #1456 (comment)

@priyadarshan
Copy link

@mb21 Thank you, I understood that. As I said, using line blocks syntax for verse would not be compatible with our guideline of keeping original text as clean and intact as possible. That is even more so for poetry.

Thank you for pointer to rewriting divs in LaTeX. That is exactly what we need. I hope that ticket will be completed too, it seems nice to have same possibility in LaTeX as in HTML writers.

Apology for creating noise in this ticket. After all, that is about asciidoc.

Thank you so much to John MacFarlane and developers for the wonderful Pandoc!

@jgm
Copy link
Owner

jgm commented May 1, 2018

Note that content in fenced divs will be parsed as Markdown before it gets to your filter. So, to get line breaks between lines of verse, you'd have to insert them manually (two space or backslash at end of line) or enable the +hard_line_breaks extension globally. Another option would be to use a code block and do all the processing in the filter (splitting into lines and parsing as Markdown). For block quotes, it would be quite easy to use a filter to create a fenced syntax as you describe.

If you have further discussion on this topic, though, please move it to pandoc-discuss.

@priyadarshan
Copy link

Thank you so much for the valuable advice.

If you have further discussion on this topic, though, please move it to pandoc-discuss.

Thank you, I will.

@JamesRandom
Copy link

An asciidoc reader would also be very useful to me (to convert from asciidoc to rst)

@tajmone
Copy link
Contributor

tajmone commented Jan 15, 2021

Regarding the DIVs problem mentioned above, I wanted to add that there's a new third party semantic HTML backend for Asciidoctor now (if I remember correctly, it was added after this thread began):

https://github.com/jirutka/asciidoctor-html5s

So this might be a viable workaround for the problems mentioned above concerning ePubs creation.

@ciampix
Copy link

ciampix commented Jan 25, 2021

I have started working on this, it might not be ready for a few months.

2014 ... months? ;-)

@mb21
Copy link
Collaborator

mb21 commented Jun 19, 2021

@gmarpons just posted asciidoc-hs on pandoc-discuss:

It is meant to be both a library that some day could be integrated as a
Pandoc dependency (similarly to commonmark-hs) and a separated executable.

@alerque
Copy link
Contributor

alerque commented Jun 20, 2021

@ciampix Open Source Months are a fairly flexible unit of measure, each month being roughly equivalent to one promise on the more commercial 6-8 weeks scale.

@RichardJECooke
Copy link

What feature branch is this issue in the github code please? I can't see any branch related to 1456 or asciidoc.

@jgm
Copy link
Owner

jgm commented Apr 30, 2022

There's this library being developed independently:
https://github.com/gmarpons/asciidoc-hs
If it were finished, we could use this to add asciidoc support to pandoc. You might inquire there about how you might help.

@noraj
Copy link

noraj commented Jun 6, 2022

I'm confused.

https://pandoc.org/demos.html 28.

AsciiDoc:

pandoc -s MANUAL.txt -t asciidoc -o example28.txt

Why it supports only .txt extension and if you pass .adoc it triggers an error: Unknown input format asciidoc?

Also if as output I set example.pdf it will trigger cannot produce pdf output from asciidoc which is expected if it's not supported but if I put example.docx it will generate a docx file containing the Asciidoc document (same as txt output) in a docx and not something converted so if docx is not supported it should trigger an error too.

@tarleb
Copy link
Collaborator

tarleb commented Jun 6, 2022

Pandoc assumes that files with .txt extension contain Markdown, while those with a .adoc extension contain Asciidoc. However, while pandoc can write Asciidoc, it cannot read it. Hence this issue.

I'm not sure what you are asking in the second paragraph. Please raise this on the pandoc-discuss mailing list. We want to reserve this tracker for bug reports and feature requests, questions should go to the mailing list.

@VladimirAlexiev
Copy link
Contributor

@jgm
Copy link
Owner

jgm commented Dec 6, 2024

We don't have to be convinced that it would be good to have an asciidoc reader. What is needed is just for someone to contribute one. asciidoc-hs noted above might be a good starting point if someone wants to take this on.

In the mean time, you can probably get good results using asciidoctor to convert to HTML or DocBook and then converting the result using pandoc to your target format.

@tajmone
Copy link
Contributor

tajmone commented Dec 6, 2024

Is It Viable?

Since I use both pandoc and AsciiDoc (Asciidoctor Ruby) on a daily basis I'd really love to see a native AsciiDoc reader in pandoc, but ... having given it quite a lot of though, I'm just wondering whether it's viable.

The AsciiDoc syntax is much richer and broader than pandoc, so it stands to reason that except for simple documents it will tend to be a lossy conversion. Which makes me wonder if it wouldn't make sense to approach the issue the other way round, and create a dedicated pandoc AsciiDoc backend instead. There are currently various AsciiDoc incarnations (Python, Ruby, JS, Java) and some of them might offer an easier approach to this task — indeed, the Haskell library mentioned by @jgm (gmarpons/asciidoc-hs) would pave the path to possibly including a full-fledged AsciiDoc parser into pandoc.

I did look into this for Asciidoctor Ruby, but at the time the AST wasn't documented enough to make the task easy. I'm not sure if the situation has changed, but once you have access the AsciiDoc document in AST format, converting it to a pandoc AST should be fairly straightforward (but still be a lossy conversion, e.g. in case of book Parts, callouts, and other features not present in pandoc native elements).

So, ideally, the creation of an AsciiDoc backend capable of emitting a pandoc AST would solve the problem, even though it might not be as practical as having pandoc natively support AsciiDoc as an input format.

What I'm trying to say is that, given that in many cases the conversion could be lossy, it might make more sense to handle it on the AsciiDoc side, which might make it easier to control how unsupported elements are to be rendered (or ignored) during conversion. Having to control such settings from within pandoc might be rather complicated, and surely not something that could be easily handled via command line switches (maybe a YAML settings file? or a dedicated defaults file?).

The Haskell AsciiDoc library could indeed be a game changer in this respect, since it would allow inclusion of a real AsciiDoc parser into pandoc. But even so, the question of how to handle unsupported elements would have to be faced.

I've used various intermediate formats as a workaround for AsciiDoc to pandoc conversion, but the issues with unsupported features (and what to do with/to them) remains. DocBook is probably the closest syntax to AsciiDoc, and for simple documents it works pretty well out of the box.

@sjfloat
Copy link

sjfloat commented Dec 7, 2024 via email

@jgm
Copy link
Owner

jgm commented Dec 7, 2024

Sure, many formats are lossy, as explained at the beginning of the manual. Pandoc just does its best. And we can do a lot with Div and Span elements in the AST; with these, we can probably capture most or perhaps all of the semantic information in an asciidoc document.

@VladimirAlexiev
Copy link
Contributor

@tajmone thanks for the information! Is there a comparison between their respective features?

@tajmone
Copy link
Contributor

tajmone commented Dec 15, 2024

@tajmone thanks for the information! Is there a comparison between their respective features?

I can't think of a single document with a direct comparison of features, especially the pandoc Markdown flavor vs AsciiDoc. The AsciiDoc Language Documentation is a good source of information when it comes to AsciiDoc features, and contains various Appendixes and summaries which come in handy.

A main obstacle that comes to mind is the level of book partitioning supported by AsciiDoc, which includes book parts and special sections, for which pandoc doesn't have any equivalent native elements, so I don't see how such documents could be converted without loosing the book structure. In theory, it should be possible to use Lua filters to emulate book parts, e.g. by using Level 1 headings as Part titles and shifting down one level all chapter titles, etc., e.g. in chunked documents, but this would also require to manually rebuild the ToC to keep into account how headings are shifted within book parts, compared to sections which are not inside parts (Introduction, Appendices, etc.). But then, since there are many possible ways to organize such book partitioning schemes, it might be hard to find a unified solution.

But even for simple documents, elements like admonitions, the various block types supported by AsciiDoc, callouts in code listings, etc., will be problematic to render in pandoc without agreeing on some non-native ways to represent them, according to format (e.g. in HTML admonitions could be represented by divs with agreed-upon classes) and elements like callouts might require dedicated Lua filters in ordered to be recreated in pandoc markdown and be usable in different output formats. Similar choices would inevitable be arbitrary and somewhat opinionated, unless the list of native elements is expanded to officially accommodate them as first class citizens of the pandoc AST.

AsciiDoc also relies quite heavily on attributes (native or custom) which allow conditional behaviors via preprocessor directives, e.g. to include or exclude certain blocks of contents, or to dynamically include external files (recursively). It's somewhat similar to how pandoc templates work, except that it takes place directly inside the source document, whereas with pandoc similar functionality only applies to templates, not the body text.

A native pandoc reader would have to resolve all the preprocessing before conversion, which seems quite a lot of work and its the reason why I think it might make more sense to be handling it from within AsciiDoc itself, since a same source project might produce different results depending on the output format (chunked HTML, EPub, DocBook, etc.), so preserving the various preprocessor directives allows better control over different editions.

The real problem here is that in order to be able to properly support AsciiDoc, the list of pandoc native elements would have to be expanded to include book parts, special sections, and possibly support admonitions. These are not the only elements available in AsciiDoc and absent in pandoc, but they are definitely good examples of commonly used elements which are going to be problematic to handle.

@jgm
Copy link
Owner

jgm commented Dec 15, 2024

As I understand it, AsciiDoc's document model is a subset of DocBook's. We support DocBook as an input format, so there is no reason we couldn't support AsciiDoc -- and the same sorts of tradeoffs and workarounds would be needed.

If someone is interested in doing this, the best way forward would be to develop a separate Haskell library with an AST that is tailored to AsciiDoc. Pandoc could then add an AsciiDoc reader that calls this library's parser and then "downshifts" from its AST to the Pandoc AST. The library could also be used independently for people who want the Asciidoc-tailored AST.

In the mean time, I think you should get decent results by using asciidoctor to produce docbook and then converting that with pandoc.

@sorairolake
Copy link

The following example generates Markdown with headings shifted down one level (e.g., <h3> to <h2>):

asciidoctor -b docbook5 -o - README.adoc | pandoc -f docbook -t markdown

To generate Markdown with the same heading level as AsciiDoc, you need to specify leveloffset=+1 as in the example below (see asciidoctor/asciidoctor#1907 (comment)):

asciidoctor -b docbook5 -o - -a leveloffset=+1 README.adoc | pandoc -f docbook -t markdown

I think this is a bit inconvenient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests