diff options
author | Robert Alessi <alessi@robertalessi.net> | 2020-04-28 16:51:04 +0200 |
---|---|---|
committer | Robert Alessi <alessi@robertalessi.net> | 2020-04-28 16:51:04 +0200 |
commit | 2dffe397f9a027ae551037e6f1c0b27e2350bf08 (patch) | |
tree | 3639519acb611b539f9ecac0f8539c1f6e1b94dd | |
parent | d60e584fd104a46dcc8b59f4d18dd50548526310 (diff) | |
download | ekdosis-2dffe397f9a027ae551037e6f1c0b27e2350bf08.tar.gz |
handle inconsistently numbered divs
-rw-r--r-- | ekdosis.dtx | 62 |
1 files changed, 44 insertions, 18 deletions
diff --git a/ekdosis.dtx b/ekdosis.dtx index 5ec9df1..e46a6d5 100644 --- a/ekdosis.dtx +++ b/ekdosis.dtx | |||
@@ -1843,7 +1843,9 @@ local function section_totei(str) | |||
1843 | return str | 1843 | return str |
1844 | end | 1844 | end |
1845 | 1845 | ||
1846 | local function close_divs_at_end(str) | 1846 | local used_ndivs = {} |
1847 | |||
1848 | local function close_ndivs_at_end(str) | ||
1847 | local isdiv = false | 1849 | local isdiv = false |
1848 | if string.find(str, "</div>$") | 1850 | if string.find(str, "</div>$") |
1849 | then | 1851 | then |
@@ -1851,17 +1853,36 @@ local function close_divs_at_end(str) | |||
1851 | str = string.gsub(str, "(.*)(</div>)$", "%1") | 1853 | str = string.gsub(str, "(.*)(</div>)$", "%1") |
1852 | else | 1854 | else |
1853 | end | 1855 | end |
1854 | firstdiv = string.match(str, "<div%d") or "" | 1856 | -- collect used div numbers |
1855 | firstdiv = tonumber(string.match(firstdiv, "%d")) or "" | 1857 | for i in string.gmatch(str, "<div%d") |
1856 | lastdiv = string.match(string.reverse(str), "%dvid<") or "" | 1858 | do |
1857 | lastdiv = tonumber(string.match(lastdiv, "%d")) or "" | 1859 | i = string.match(i, "%d") |
1860 | if isintable(used_ndivs, i) | ||
1861 | then | ||
1862 | else | ||
1863 | table.insert(used_ndivs, {a = i} ) | ||
1864 | end | ||
1865 | end | ||
1866 | if next(used_ndivs) ~= nil | ||
1867 | then | ||
1868 | table.sort(used_ndivs, function(a ,b) return(#a.a > #b.a) end) | ||
1869 | else | ||
1870 | end | ||
1871 | local firstdiv = string.match(str, "<div%d") or "" | ||
1872 | firstdiv = string.match(firstdiv, "%d") or "" | ||
1873 | local lastdiv = string.match(string.reverse(str), "%dvid<") or "" | ||
1874 | lastdiv = string.match(lastdiv, "%d") or "" | ||
1875 | local firstdivindex = get_a_index(firstdiv, used_ndivs) | ||
1876 | local lastdivindex = get_a_index(lastdiv, used_ndivs) | ||
1877 | firstdivindex = tonumber(firstdivindex) | ||
1878 | lastdivindex = tonumber(lastdivindex) | ||
1858 | local closedivs = "" | 1879 | local closedivs = "" |
1859 | if tonumber(firstdiv) | 1880 | if isintable(used_ndivs, firstdiv) |
1860 | then | 1881 | then |
1861 | while lastdiv >= firstdiv | 1882 | while lastdivindex >= firstdivindex |
1862 | do | 1883 | do |
1863 | closedivs = closedivs.."</div"..lastdiv..">" | 1884 | closedivs = closedivs.."</div"..used_ndivs[lastdivindex].a..">" |
1864 | lastdiv = lastdiv - 1 | 1885 | lastdivindex = lastdivindex - 1 |
1865 | end | 1886 | end |
1866 | end | 1887 | end |
1867 | -- str = string.gsub(str, "(<div)(%d)(.->)", "%1%3") | 1888 | -- str = string.gsub(str, "(<div)(%d)(.->)", "%1%3") |
@@ -1873,20 +1894,24 @@ local function close_divs_at_end(str) | |||
1873 | end | 1894 | end |
1874 | end | 1895 | end |
1875 | 1896 | ||
1876 | local function close_divs_in_between(str) | 1897 | local function close_ndivs_in_between(str) |
1877 | for ndivi = 0, 9 | 1898 | for ndivi = 0, 9 |
1878 | do | 1899 | do |
1879 | str = string.gsub(str, "(<div)("..ndivi..")(.->)(.-)(<div)(%d)(.->)", | 1900 | str = string.gsub(str, "(<div)("..ndivi..")(.->)(.-)(<div)(%d)(.->)", |
1880 | function(bdivi, ndivi, edivi, between, bdivii, ndivii, edivii) | 1901 | function(bdivi, ndivi, edivi, between, bdivii, ndivii, edivii) |
1881 | firstdiv = tonumber(ndivi) | 1902 | local firstdiv = ndivi |
1882 | lastdiv = tonumber(ndivii) | 1903 | local lastdiv = ndivii |
1904 | local firstdivindex = get_a_index(firstdiv, used_ndivs) | ||
1905 | local lastdivindex = get_a_index(lastdiv, used_ndivs) | ||
1906 | firstdivindex = tonumber(firstdivindex) | ||
1907 | lastdivindex = tonumber(lastdivindex) | ||
1883 | local closedivs = "" | 1908 | local closedivs = "" |
1884 | if firstdiv >= lastdiv | 1909 | if firstdivindex >= lastdivindex |
1885 | then | 1910 | then |
1886 | while firstdiv >= lastdiv | 1911 | while firstdivindex >= lastdivindex |
1887 | do | 1912 | do |
1888 | closedivs = closedivs.."</div"..firstdiv..">" | 1913 | closedivs = closedivs.."</div"..used_ndivs[firstdivindex].a..">" |
1889 | firstdiv = firstdiv - 1 | 1914 | firstdivindex = firstdivindex - 1 |
1890 | end | 1915 | end |
1891 | end | 1916 | end |
1892 | return string.format("%s%s%s%s%s%s%s%s", | 1917 | return string.format("%s%s%s%s%s%s%s%s", |
@@ -1894,6 +1919,7 @@ local function close_divs_in_between(str) | |||
1894 | 1919 | ||
1895 | end) | 1920 | end) |
1896 | end | 1921 | end |
1922 | used_ndivs = {} | ||
1897 | return str | 1923 | return str |
1898 | end | 1924 | end |
1899 | 1925 | ||
@@ -1911,8 +1937,8 @@ local function textotei(str) | |||
1911 | str = self_close_tags(str) | 1937 | str = self_close_tags(str) |
1912 | str = partotei(str) | 1938 | str = partotei(str) |
1913 | str = checkpars(str) | 1939 | str = checkpars(str) |
1914 | str = close_divs_at_end(str) | 1940 | str = close_ndivs_at_end(str) |
1915 | str = close_divs_in_between(str) | 1941 | str = close_ndivs_in_between(str) |
1916 | return str | 1942 | return str |
1917 | end | 1943 | end |
1918 | 1944 | ||