diff options
author | Robert Alessi <alessi@robertalessi.net> | 2020-04-26 19:47:27 +0200 |
---|---|---|
committer | Robert Alessi <alessi@robertalessi.net> | 2020-04-26 19:47:27 +0200 |
commit | b3d7ebd008cea5f2c9fc532316ac9051650e67d8 (patch) | |
tree | aabfb72e495543e5c6ae8bfb6d0aaf63409848f3 /ekdosis.dtx | |
parent | 4145508cdf18110cc2d1dc47d9a9d86e3e60469d (diff) | |
download | ekdosis-b3d7ebd008cea5f2c9fc532316ac9051650e67d8.tar.gz |
convert LaTeX sectioning commands to embedded TEI divs equivalents
Diffstat (limited to 'ekdosis.dtx')
-rw-r--r-- | ekdosis.dtx | 85 |
1 files changed, 80 insertions, 5 deletions
diff --git a/ekdosis.dtx b/ekdosis.dtx index 63b3b75..5c142e2 100644 --- a/ekdosis.dtx +++ b/ekdosis.dtx | |||
@@ -1193,6 +1193,10 @@ local note = lpeg.Cs("note") | |||
1193 | local lnbrk = lpeg.Cs("\\\\") | 1193 | local lnbrk = lpeg.Cs("\\\\") |
1194 | local poemline = lpeg.Cs(lnbrk * bsqbrackets^-1) | 1194 | local poemline = lpeg.Cs(lnbrk * bsqbrackets^-1) |
1195 | local endpoem = lpeg.Cs(lnbrk * lpeg.S("*!") * bsqbrackets^-1) | 1195 | local endpoem = lpeg.Cs(lnbrk * lpeg.S("*!") * bsqbrackets^-1) |
1196 | local sections = lpeg.Cs(lpeg.P("book") + lpeg.P("part") + lpeg.P("chapter") | ||
1197 | + lpeg.P("section") + lpeg.P("subsection") | ||
1198 | + lpeg.P("subsubsection") + lpeg.P("paragraph") | ||
1199 | + lpeg.P("subparagraph")) | ||
1196 | 1200 | ||
1197 | -- Bind to local variables | 1201 | -- Bind to local variables |
1198 | local next = next | 1202 | local next = next |
@@ -1661,12 +1665,12 @@ local function envtotei(str) | |||
1661 | then | 1665 | then |
1662 | str = gsub(str, (lpeg.P("\\par") + lpeg.P("<p>")) * spcenc^-1 * lpeg.P("\\begin") * spcenc^-1 * lpeg.P("{") | 1666 | str = gsub(str, (lpeg.P("\\par") + lpeg.P("<p>")) * spcenc^-1 * lpeg.P("\\begin") * spcenc^-1 * lpeg.P("{") |
1663 | * lpeg.Cs(envtotags[i].a) * lpeg.P("}") | 1667 | * lpeg.Cs(envtotags[i].a) * lpeg.P("}") |
1664 | * bsqbracketsii * bcbracesii * spcenc^-1, | 1668 | * bcbracesii * spcenc^-1, |
1665 | "\n<"..envtotags[i].b..envtotags[i].c..">") | 1669 | "\n<"..envtotags[i].b..envtotags[i].c..">") |
1666 | else | 1670 | else |
1667 | str = gsub(str, (lpeg.P("\\par") + lpeg.P("<p>")) * spcenc^-1 * lpeg.P("\\begin") * spcenc^-1 * lpeg.P("{") | 1671 | str = gsub(str, (lpeg.P("\\par") + lpeg.P("<p>")) * spcenc^-1 * lpeg.P("\\begin") * spcenc^-1 * lpeg.P("{") |
1668 | * lpeg.Cs(envtotags[i].a) * lpeg.P("}") | 1672 | * lpeg.Cs(envtotags[i].a) * lpeg.P("}") |
1669 | * bsqbracketsii * bcbracesii * spcenc^-1, | 1673 | * bcbracesii * spcenc^-1, |
1670 | "</p>\n<"..envtotags[i].b..envtotags[i].c..">") | 1674 | "</p>\n<"..envtotags[i].b..envtotags[i].c..">") |
1671 | end | 1675 | end |
1672 | str = gsub(str, spcenc^-1 * lpeg.P("\\end") * spcenc^-1 * lpeg.P("{") | 1676 | str = gsub(str, spcenc^-1 * lpeg.P("\\end") * spcenc^-1 * lpeg.P("{") |
@@ -1674,7 +1678,7 @@ local function envtotei(str) | |||
1674 | "</"..envtotags[i].b..">\n<p>") | 1678 | "</"..envtotags[i].b..">\n<p>") |
1675 | str = gsub(str, lpeg.P("\\begin") * spcenc^-1 * lpeg.P("{") | 1679 | str = gsub(str, lpeg.P("\\begin") * spcenc^-1 * lpeg.P("{") |
1676 | * lpeg.Cs(envtotags[i].a) * lpeg.P("}") | 1680 | * lpeg.Cs(envtotags[i].a) * lpeg.P("}") |
1677 | * bsqbracketsii * bcbracesii * spcenc^-1, | 1681 | * bcbracesii * spcenc^-1, |
1678 | "<"..envtotags[i].b..envtotags[i].c..">") | 1682 | "<"..envtotags[i].b..envtotags[i].c..">") |
1679 | str = gsub(str, spcenc^-1 * lpeg.P("\\end") * spcenc^-1 * lpeg.P("{") | 1683 | str = gsub(str, spcenc^-1 * lpeg.P("\\end") * spcenc^-1 * lpeg.P("{") |
1680 | * lpeg.Cs(envtotags[i].a) * lpeg.P("}"), | 1684 | * lpeg.Cs(envtotags[i].a) * lpeg.P("}"), |
@@ -1682,7 +1686,7 @@ local function envtotei(str) | |||
1682 | else | 1686 | else |
1683 | str = gsub(str, lpeg.P("\\begin") * spcenc^-1 * lpeg.P("{") | 1687 | str = gsub(str, lpeg.P("\\begin") * spcenc^-1 * lpeg.P("{") |
1684 | * lpeg.Cs(envtotags[i].a) * lpeg.P("}") | 1688 | * lpeg.Cs(envtotags[i].a) * lpeg.P("}") |
1685 | * bsqbracketsii * bcbracesii * spcenc^-1, | 1689 | * bcbracesii * spcenc^-1, |
1686 | "<"..envtotags[i].b..envtotags[i].c..">") | 1690 | "<"..envtotags[i].b..envtotags[i].c..">") |
1687 | str = gsub(str, spcenc^-1 * lpeg.P("\\end") * spcenc^-1 * lpeg.P("{") | 1691 | str = gsub(str, spcenc^-1 * lpeg.P("\\end") * spcenc^-1 * lpeg.P("{") |
1688 | * lpeg.Cs(envtotags[i].a) * lpeg.P("}"), | 1692 | * lpeg.Cs(envtotags[i].a) * lpeg.P("}"), |
@@ -1691,7 +1695,7 @@ local function envtotei(str) | |||
1691 | else | 1695 | else |
1692 | str = gsub(str, lpeg.P("\\begin") * spcenc^-1 * lpeg.P("{") | 1696 | str = gsub(str, lpeg.P("\\begin") * spcenc^-1 * lpeg.P("{") |
1693 | * lpeg.Cs(envtotags[i].a) * lpeg.P("}") | 1697 | * lpeg.Cs(envtotags[i].a) * lpeg.P("}") |
1694 | * bsqbracketsii * bcbracesii * spcenc^-1, | 1698 | * bcbracesii * spcenc^-1, |
1695 | "") | 1699 | "") |
1696 | str = gsub(str, spcenc^-1 * lpeg.P("\\end") * spcenc^-1 * lpeg.P("{") | 1700 | str = gsub(str, spcenc^-1 * lpeg.P("\\end") * spcenc^-1 * lpeg.P("{") |
1697 | * lpeg.Cs(envtotags[i].a) * lpeg.P("}"), | 1701 | * lpeg.Cs(envtotags[i].a) * lpeg.P("}"), |
@@ -1770,6 +1774,8 @@ local function partotei(str) | |||
1770 | str = string.gsub(str, "<p>%s-</p>", "") | 1774 | str = string.gsub(str, "<p>%s-</p>", "") |
1771 | str = string.gsub(str, "(%<p%>)%s-(%</div%>)$", "%2") | 1775 | str = string.gsub(str, "(%<p%>)%s-(%</div%>)$", "%2") |
1772 | str = string.gsub(str, "(%<p%>)%s-$", "") | 1776 | str = string.gsub(str, "(%<p%>)%s-$", "") |
1777 | str = string.gsub(str, "(<p>)%s-(<div.->)", "%2%1") | ||
1778 | str = string.gsub(str, "(%<p%>)%s-(<head>.-</head>)", "%2%1") | ||
1773 | return str | 1779 | return str |
1774 | end | 1780 | end |
1775 | 1781 | ||
@@ -1812,6 +1818,72 @@ local function self_close_tags(str) | |||
1812 | return str | 1818 | return str |
1813 | end | 1819 | end |
1814 | 1820 | ||
1821 | local divdepth = { | ||
1822 | book = 0, | ||
1823 | part = 1, | ||
1824 | chapter = 2, | ||
1825 | section = 3, | ||
1826 | subsection = 4, | ||
1827 | subsubsection = 5, | ||
1828 | paragraph = 6, | ||
1829 | subparagraph = 7 | ||
1830 | } | ||
1831 | |||
1832 | local function section_totei(str) | ||
1833 | str = gsub(str, dblbkslash * sections * spce^-1 * bcbraces, "%1%2%3[]%4") | ||
1834 | str = gsub(str, dblbkslash * sections * spce^-1 * bsqbrackets * bcbraces, | ||
1835 | function(bkslash, secname, space, opt, arg) | ||
1836 | ctr = divdepth[secname] | ||
1837 | arg = string.sub(arg, 2, -2) | ||
1838 | -- return string.format("\\par <div%s type=\"%s\" depth=\"%s\"><head>%s</head>", | ||
1839 | -- ctr, secname, ctr, arg) | ||
1840 | return string.format("\\par <div%s type=\"%s\"><head>%s</head>", | ||
1841 | ctr, secname, arg) | ||
1842 | end) | ||
1843 | return str | ||
1844 | end | ||
1845 | |||
1846 | local function close_divs_at_end(str) | ||
1847 | local isdiv = false | ||
1848 | if string.find(str, "</div>$") | ||
1849 | then | ||
1850 | isdiv = true | ||
1851 | str = string.gsub(str, "(.*)(</div>)$", "%1") | ||
1852 | else | ||
1853 | end | ||
1854 | firstdiv = string.match(str, "<div%d") or "" | ||
1855 | firstdiv = tonumber(string.match(firstdiv, "%d")) or "" | ||
1856 | lastdiv = string.match(string.reverse(str), "%dvid<") or "" | ||
1857 | lastdiv = tonumber(string.match(lastdiv, "%d")) or "" | ||
1858 | local closedivs = "" | ||
1859 | if tonumber(firstdiv) | ||
1860 | then | ||
1861 | while lastdiv >= firstdiv | ||
1862 | do | ||
1863 | closedivs = closedivs.."</div"..lastdiv..">" | ||
1864 | lastdiv = lastdiv - 1 | ||
1865 | end | ||
1866 | end | ||
1867 | -- str = string.gsub(str, "(<div)(%d)(.->)", "%1%3") | ||
1868 | if isdiv | ||
1869 | then | ||
1870 | return str..closedivs.."</div>" | ||
1871 | else | ||
1872 | return str..closedivs | ||
1873 | end | ||
1874 | end | ||
1875 | |||
1876 | local function close_divs_in_between(str) | ||
1877 | for i = 0, 9 | ||
1878 | do | ||
1879 | str = string.gsub(str, "(<div"..i..".->)(.-)(<div"..i..".->)", function(divi, between, divii) | ||
1880 | between = close_divs_in_between(between) | ||
1881 | return string.format("%s%s</div%s>%s", divi, between, i, divii) | ||
1882 | end) | ||
1883 | end | ||
1884 | return str | ||
1885 | end | ||
1886 | |||
1815 | local function textotei(str) | 1887 | local function textotei(str) |
1816 | str = xml_entities(str) | 1888 | str = xml_entities(str) |
1817 | str = texpatttotei(str) | 1889 | str = texpatttotei(str) |
@@ -1821,10 +1893,13 @@ local function textotei(str) | |||
1821 | str = relocate_notes(str) | 1893 | str = relocate_notes(str) |
1822 | str = versetotei(str) | 1894 | str = versetotei(str) |
1823 | str = envtotei(str) | 1895 | str = envtotei(str) |
1896 | str = section_totei(str) | ||
1824 | str = cmdtotei(str) | 1897 | str = cmdtotei(str) |
1825 | str = self_close_tags(str) | 1898 | str = self_close_tags(str) |
1826 | str = partotei(str) | 1899 | str = partotei(str) |
1827 | str = checkpars(str) | 1900 | str = checkpars(str) |
1901 | str = close_divs_at_end(str) | ||
1902 | str = close_divs_in_between(str) | ||
1828 | return str | 1903 | return str |
1829 | end | 1904 | end |
1830 | 1905 | ||