jgm / pandoc

Universal markup converter
https://pandoc.org
Other
34.89k stars 3.39k forks source link

Provide a way (or documentation if it exists) to change the HTML output of epub conversion #8446

Closed Samreay closed 2 years ago

Samreay commented 2 years ago

Describe your proposed improvement and the problem it solves.

I am currently trying to export an epub from the markdown files I have written. Due to the epub standard not supporting many good CSS features, I'd like to modify the HTML output.

For example, being able to specify "When you have a chapter title, please use this block of HTML instead of just an h1 tag".

Describe alternatives you've considered.

I tried modifying the default.epub3 template, but everything I need to change actually seems abstracted away inside $body, and I don't know how to modify that. From more documentation reading I'm unsure if this would require a brand new lua exporter, and if I have to copy/paste an original implementation of the epub converter from somewhere in the repo (sorry, I wasn't able to find it), or if there's a way where I can say "Use the original exporter, but if you find a heading 1 with this type, do this instead."

Any help would be greatly apprciated.

tarleb commented 2 years ago

A Lua filter could be used to do this. Can you give an example of the modifications you want to make?

Samreay commented 2 years ago

Sure, so for example when inserting a title, instead of

<h1>title<h1/>

I might want an image, a background image, or something similar to turn the above into

<div class="centre">
    <h1> title</h1>
    <img src="someimg.jpg">
</div>

Another use case would be chapters with styled subheadings, which I've tried to get working just with CSS but had minimal luck.

Screenshot_20221123-071414

tarleb commented 2 years ago

Here's some Lua code that should do the above, minus the image. Raw HTML can be added via RawBlocks and RawInline.

function Header (h)
  if h.level == 1 then
    return pandoc.Div({h}, {class="center"})
  end
end

It may take some playing around, but should be well suited for the job.

Samreay commented 2 years ago

Apologies, but are you able to provide some more context at to how to use this script? Do you pass it in via pandocs cli?

jgm commented 2 years ago

Save it as fancyheadings.lua and then use --lua-filter fancyheadings.lua on the command line.