Last active
November 10, 2021 23:50
-
-
Save bpj/496d5ca188d7354e54e5d48fa386df53 to your computer and use it in GitHub Desktop.
Pandoc Lua filter which replaces \TeX, \LaTeX etc. logos with appropriate HTML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
# pandoc-texlogo2html.lua -- convert `\TeX` logo and friends to HTML | |
TL;DR: type `\LaTeX` in your Pandoc Markdown document and get sensible HTML! | |
This Pandoc filter replaces raw tex/latex blocks and inlines whose content | |
are tex logo commands like `\TeX`, `\LaTeX`, `\XeLaTeX`, `\LuaTeX` etc. | |
(The ones shown in the tex_logos table below, you can easily add more!) | |
with a raw block/inline with `html` as format and an HTML string which | |
when styled with the CSS below becomes an approximation of the respective | |
logo as text. When CSS styling isn't available they will at least | |
be recognisable... | |
Usage: | |
lua pandoc-texlogo2html.lua css >texlogo.css | |
pandoc --lua-filter pandoc-texlogo2html.lua --css texlogo.css -t html5 -o output.html input.md | |
If you don't have the standalone lua command just copy the CSS below to a | |
file texlogo.css | |
The HTML and CSS was modified from that found at | |
<http://nitens.org/taraborelli/texlogo> | |
In case you wonder why I replaced `<sub>` and `<sup>` with spans please | |
compare the two paragraphs below in your browser with styling turned off: | |
```html | |
<p>T<span class="sub">e</span>X</p> | |
<p>T<sub>e</sub>X</p> | |
``` | |
I know which one I prefer! Feel free to change things (back) if you differ. | |
I would have preferred to use `ɘ` instead of `Ǝ` in `Xe`. | |
Unfortunately `text-transform: uppercase` seems not to work for (most) | |
non-ASCII letters in most browsers! C'mon, this is 2018! :-( | |
]] | |
-- If you don't have the standalone lua command just copy the CSS below to a | |
-- file texlogo.css | |
if arg ~= nil and arg[1] == 'css' then | |
print([[ | |
.texlogo { | |
/* font-family: Times, "Times New Roman", serif; */ | |
letter-spacing: 1px; | |
} | |
.texlogo .sup { | |
text-transform: uppercase; | |
letter-spacing: 1px; | |
font-size: 0.85em; | |
vertical-align: 0.15em; | |
margin-left: -0.36em; | |
margin-right: -0.15em; | |
} | |
.texlogo .sub { | |
text-transform: uppercase; | |
vertical-align: -0.5ex; | |
margin-left: -0.1667em; | |
margin-right: -0.125em; | |
font-size: 1em; | |
} | |
.texlogo .sc { | |
text-transform: uppercase; | |
letter-spacing: 0px; | |
font-size: 0.85em; | |
} | |
.texlogo .plain { | |
letter-spacing: 0px; | |
} | |
]]) | |
end | |
local tex_logos = { | |
['\\TeX'] = '<span class="texlogo">T<span class="sub">e</span>X</span>', | |
['\\LaTeX'] = '<span class="texlogo">L<span class="sup">a</span>T<span class="sub">e</span>X</span>', | |
['\\XeTeX'] = '<span class="texlogo">X<span class="sub">Ǝ</span>T<span class="sub">e</span>X</span>', | |
['\\XeLaTeX'] = '<span class="texlogo">X<span class="sub">Ǝ</span>L<span class="sup">a</span>T<span class="sub">e</span>X</span>', | |
['\\LuaTeX'] = '<span class="texlogo"><span class="plain">Lua</span>T<span class="sub">e</span>X</span>', | |
['\\LuaLaTeX'] = '<span class="texlogo"><span class="plain">Lua</span>L<span class="sup">a</span>T<span class="sub">e</span>X</span>', | |
['\\ConTeXt'] = '<span class="texlogo">C<span class="sc">on</span>T<span class="sub">e</span>X<span class="sc">t</span></span>', | |
} | |
local get_raw = { | |
RawInline = function(text) return pandoc.RawInline('html', text) end, | |
RawBlock = function(text) return pandoc.RawBlock('html', '<p>' .. text .. '</p>') end, | |
} | |
local function logo2html(raw) | |
local format = raw.format | |
if format == 'tex' or format == 'latex' then | |
local logo = tex_logos[raw.text] | |
if logo ~= nil then | |
return get_raw[raw.t](logo) | |
else | |
return raw | |
end | |
else | |
return raw | |
end | |
end | |
return { { RawBlock = logo2html, RawInline = logo2html } } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment