diff options
-rw-r--r-- | arabluatex.lua | 195 |
1 files changed, 181 insertions, 14 deletions
diff --git a/arabluatex.lua b/arabluatex.lua index 3efccd2..4128955 100644 --- a/arabluatex.lua +++ b/arabluatex.lua | |||
@@ -50,7 +50,7 @@ local bsqbracketsii = lpeg.Cs(bsqbrackets^-2) | |||
50 | local bcbracesii = lpeg.Cs(bcbraces^-2) | 50 | local bcbracesii = lpeg.Cs(bcbraces^-2) |
51 | local cmd = lpeg.Cs(dblbkslash * ascii^1 * cmdstar^-1) | 51 | local cmd = lpeg.Cs(dblbkslash * ascii^1 * cmdstar^-1) |
52 | local cmdargs = lpeg.Cs(spce * bsqbracketsii * bcbracesii * bsqbrackets^-1) | 52 | local cmdargs = lpeg.Cs(spce * bsqbracketsii * bcbracesii * bsqbrackets^-1) |
53 | local arbargs = lpeg.Cs(spce * bsqbrackets^-1 * bcbraces) | 53 | local arbargs = lpeg.Cs(spce^-1 * bsqbrackets^-1 * bcbraces) |
54 | local baytargs = lpeg.Cs(spce * bcbraces * bsqbrackets^-1 * bcbraces) | 54 | local baytargs = lpeg.Cs(spce * bcbraces * bsqbrackets^-1 * bcbraces) |
55 | 55 | ||
56 | local function protectarb(str) | 56 | local function protectarb(str) |
@@ -641,24 +641,163 @@ local function processbuckw(str) | |||
641 | return str | 641 | return str |
642 | end | 642 | end |
643 | 643 | ||
644 | function processexport(str) | 644 | --- *** |
645 | str = "\\tooutfile{"..str.."}" | 645 | local utffilesuffix = "_out" |
646 | str = gsub(str, lpeg.Cs("\\arb") * arbargs, "}%1%2\\tooutfile{") | 646 | local export_utf = "no" |
647 | str = gsub(str, lpeg.Cs("\\bayt") * baytargs, "}\\tof{\\bayt}%1%2\\tooutfile{") | 647 | |
648 | function al_utffilesuffix(str) | ||
649 | utffilesuffix = str | ||
650 | return true | ||
651 | end | ||
652 | |||
653 | function al_doexport(str) | ||
654 | export_utf = str | ||
655 | return true | ||
656 | end | ||
657 | |||
658 | function al_openstream() | ||
659 | local f = io.open(tex.jobname..utffilesuffix.."_tmp.tex", "a+") | ||
660 | local preamble = io.open(tex.jobname..".tex", "r") | ||
661 | for line in preamble:lines() do | ||
662 | f:write(line, "\n") | ||
663 | if string.find(line, "^%s-\\begin%s?{document}") then | ||
664 | break | ||
665 | end | ||
666 | end | ||
667 | preamble:close() | ||
668 | f:close() | ||
669 | return true | ||
670 | end | ||
671 | |||
672 | local function processarbtoutf(str) | ||
673 | str = string.gsub(str, "(\\prname)%s?(%b{})", function(tag, body) | ||
674 | body = string.sub(body, 2, -2) | ||
675 | return string.format("\\arb[trans]{\\uc{%s}}", body) | ||
676 | end) | ||
677 | str = string.gsub(str, "(\\begin%s?{arab})(%b[])", "\\par\\bgroup\\arbpardir\\arb%2{") | ||
678 | str = string.gsub(str, "(\\begin%s?{arab})", "\\par\\bgroup\\arbpardir\\arb{") | ||
679 | str = string.gsub(str, "\\end%s?{arab}", "}\\egroup\\par") | ||
680 | -- str = gsub(str, lpeg.Cs("\\arb") * spcenc * bsqbrackets^-1 * bcbraces, function(tag, opt, body) | ||
681 | -- body = string.sub(body, 2, -2) | ||
682 | -- return string.format("%s%s\\@al@pr@ob%s\\@al@pr@cb", tag, opt, body) | ||
683 | -- end) | ||
684 | str = string.gsub(str, "(\\arb%s?)(%b[])(%b{})", function(tag, opt, body) | ||
685 | body = string.sub(body, 2, -2) | ||
686 | return string.format("%s%s\\@al@pr@ob%s\\@al@pr@cb", tag, opt, body) | ||
687 | end) | ||
688 | str = string.gsub(str, "(\\arb)%s?(%b{})", function(tag, body) | ||
689 | body = string.sub(body, 2, -2) | ||
690 | return string.format("%s\\@al@pr@ob%s\\@al@pr@cb", tag, body) | ||
691 | end) | ||
692 | str = string.gsub(str, "(\\arbmark)%s?(%b{})", function(tag, body) | ||
693 | body = string.sub(body, 2, -2) | ||
694 | return string.format("%s\\@al@pr@ob%s\\@al@pr@cb", tag, body) | ||
695 | end) | ||
696 | str = string.gsub(str, "(\\abjad)%s?(%b{})", function(tag, body) | ||
697 | body = string.sub(body, 2, -2) | ||
698 | return string.format("%s\\@al@pr@ob%s\\@al@pr@cb", tag, body) | ||
699 | end) | ||
700 | str = string.gsub(str, "{", "\\@al@ob ") | ||
701 | str = string.gsub(str, "}", "\\@al@cb ") | ||
702 | str = string.gsub(str, "\\@al@pr@ob", "{") | ||
703 | str = string.gsub(str, "\\@al@pr@cb", "}") | ||
704 | str = string.gsub(str, "(%b{})", function(body) | ||
705 | body = string.sub(body, 2, -2) | ||
706 | body = string.gsub(body, "\\@al@ob%s?", "{") | ||
707 | body = string.gsub(body, "\\@al@cb%s?", "}") | ||
708 | return string.format("{%s}", body) | ||
709 | end) | ||
710 | return str | ||
711 | end | ||
712 | |||
713 | function arbtoutf(str) | ||
714 | str = processarbtoutf(str) | ||
715 | str = "\\ToOutFile{"..str.."}" | ||
716 | str = string.gsub(str, "(\\ToOutFile)%s?(%b{})", function(tag, body) | ||
717 | body = string.sub(body, 2, -2) | ||
718 | body = gsub(body, lpeg.Cs("\\arb") * arbargs, "}%1%2\\ToOutFile{") | ||
719 | return string.format("%s{%s}", tag, body) | ||
720 | end) | ||
721 | str = string.gsub(str, "(\\ToOutFile)%s?(%b{})", function(tag, body) | ||
722 | body = string.sub(body, 2, -2) | ||
723 | body = string.gsub(body, "(\\abjad)%s?(%b{})", "}%1%2\\ToOutFile{") | ||
724 | return string.format("%s{%s}", tag, body) | ||
725 | end) | ||
726 | str = string.gsub(str, "(\\ToOutFile)%s?(%b{})", function(tag, body) | ||
727 | body = string.sub(body, 2, -2) | ||
728 | body = string.gsub(body, "(\\arbmark)%s?(%b{})", "}%1%2\\ToOutFile{") | ||
729 | return string.format("%s{%s}", tag, body) | ||
730 | end) | ||
731 | -- str = string.gsub(str, "(\\arb%s?)(%b[])(%b{})", "}%1%2%3\\ToOutFile{") | ||
732 | -- str = string.gsub(str, "(\\arb)%s?(%b{})", "}%1%2\\ToOutFile{") | ||
733 | -- str = gsub(str, lpeg.Cs("\\bayt") * baytargs, "}\\ToOutFile{\\bayt}%1%2\\ToOutFile{") | ||
734 | -- str = gsub(str, lpeg.Cs("\\arb") * arbargs, "}%1%2\\ToOutFile{") | ||
735 | -- str = string.gsub(str, "(\\abjad)%s?(%b{})", "}%1%2\\ToOutFile{") | ||
736 | -- str = string.gsub(str, "(\\abjad)(%s?)(%b{})", "}%1%3\\tooutfile{") | ||
737 | -- str = string.gsub(str, "(\\begin%s?{arab})", "}%1") | ||
738 | -- str = string.gsub(str, "(\\end%s?{arab})", "%1\\ToOutFile{") | ||
739 | -- str = string.gsub(str, "(\\begin)", "\n%1") | ||
740 | -- str = string.gsub(str, "(\\end)", "\n%1") | ||
741 | -- str = string.gsub(str, "(\\bayt)", "\n%1") | ||
742 | -- str = string.gsub(str, "(\\\\)", "%1\n") | ||
648 | return str | 743 | return str |
649 | end | 744 | end |
650 | 745 | ||
651 | function tooutfile(str, nl) | 746 | function tooutfile(str, nl) |
652 | local f = io.open(tex.jobname.."_out.tex", "a+") | 747 | local f = io.open(tex.jobname..utffilesuffix.."_tmp.tex", "a+") |
653 | if nl == "yes" then | 748 | if nl == "newline" then |
654 | f:write(str, "\n\n") | 749 | f:write(str, "\n\n") |
655 | else | 750 | else |
656 | f:write(str) | 751 | f:write(str) |
657 | end | 752 | end |
658 | f:close() | 753 | f:close() |
754 | return str | ||
755 | end | ||
756 | |||
757 | function al_closestream() | ||
758 | local f = io.open(tex.jobname..utffilesuffix.."_tmp.tex", "r") | ||
759 | local o = io.open(tex.jobname..utffilesuffix..".tex", "w") | ||
760 | local t = f:read("*a") | ||
761 | --[[ next lines need work! | ||
762 | -- t = string.gsub(t, "{\\textdir TRT\\arabicfont{}", "\\txarb{") | ||
763 | t = string.gsub(t, "(\\bayt%s?)\\txarb(%b{})\\txarb(%b{})\\txarb(%b{})", | ||
764 | function(tag, bodyi, bodyii, bodyiii) | ||
765 | bodyii = string.sub(bodyii, 2, -2) | ||
766 | return string.format("%s%s[%s]%s", | ||
767 | tag, bodyi, bodyii, bodyiii) | ||
768 | end) | ||
769 | t = string.gsub(t, "(\\bayt%s?)\\txarb(%b{})\\txarb(%b{})", "%1%2%3") | ||
770 | t = string.gsub(t, "{\\textdir TRT\\arbmetre{\\arabicfont{}", "\\arbmetre{\\txarb{") | ||
771 | t = string.gsub(t, "(\\bayt%s?)\\txtrans(%b{})\\txtrans(%b{})\\txtrans(%b{})", | ||
772 | function(tag, bodyi, bodyii, bodyiii) | ||
773 | bodyii = string.sub(bodyii, 2, -2) | ||
774 | return string.format("%s%s[%s]%s", | ||
775 | tag, bodyi, bodyii, bodyiii) | ||
776 | end) | ||
777 | t = string.gsub(t, "(\\bayt%s?)\\txtrans(%b{})\\txtrans(%b{})", "%1%2%3") | ||
778 | t = string.gsub(t, "\\txtrans{\\arbmetre{", "\\arbmetre{\\txtrans{") | ||
779 | t = string.gsub(t, "\\arbmetre", "\\\\\n\\hfill") | ||
780 | t = string.gsub(t, "(\\bayt)", "\n%1") | ||
781 | --]] | ||
782 | t = string.gsub(t, "\\arabicfont{}", "") | ||
783 | t = string.gsub(t, "\\par ", "\n\n") | ||
784 | t = string.gsub(t, "\\@al@ob%s?", "{") | ||
785 | t = string.gsub(t, "\\@al@cb", "}") | ||
786 | t = string.gsub(t, "\n\n\n", "\n\n") | ||
787 | if string.find(t, "\\begin%s?{document}.-\\arb") then | ||
788 | tex.print([[\unexpanded{\PackageWarningNoLine{arabluatex}{There are still 'arabtex' strings to be converted. Open ]]..tex.jobname..utffilesuffix..".tex"..[[ and compile it one more time}}]]) | ||
789 | else end | ||
790 | t = t.."\n\\end{document}" | ||
791 | io.write(t) | ||
792 | o:write(t) | ||
793 | f:close() | ||
794 | o:close() | ||
795 | os.remove(tex.jobname..utffilesuffix.."_tmp.tex") | ||
796 | -- os.rename(tex.jobname.."_out_tmp.tex", tex.jobname.."_out.tex") | ||
659 | return true | 797 | return true |
660 | end | 798 | end |
661 | 799 | ||
800 | |||
662 | function processvoc(str, rules, scheme) | 801 | function processvoc(str, rules, scheme) |
663 | str = takeoutarb(str) | 802 | str = takeoutarb(str) |
664 | str = processarbnull(str, scheme) | 803 | str = processarbnull(str, scheme) |
@@ -675,9 +814,13 @@ function processvoc(str, rules, scheme) | |||
675 | str = voc(str, rules) | 814 | str = voc(str, rules) |
676 | else end | 815 | else end |
677 | str = unprotectarb(str) | 816 | str = unprotectarb(str) |
678 | tofile = "{\\textdir TRT"..str.."}" | 817 | if export_utf == "yes" then |
679 | tooutfile(tofile) | 818 | tofile = "\\txarb{"..str.."}" |
680 | return str | 819 | tooutfile(tofile) |
820 | else | ||
821 | return str | ||
822 | end | ||
823 | return "" | ||
681 | end | 824 | end |
682 | 825 | ||
683 | function processfullvoc(str, rules, scheme) | 826 | function processfullvoc(str, rules, scheme) |
@@ -698,7 +841,13 @@ function processfullvoc(str, rules, scheme) | |||
698 | str = fullvoc(str, rules) | 841 | str = fullvoc(str, rules) |
699 | else end | 842 | else end |
700 | str = unprotectarb(str) | 843 | str = unprotectarb(str) |
701 | return str | 844 | if export_utf == "yes" then |
845 | tofile = "\\txarb{"..str.."}" | ||
846 | tooutfile(tofile) | ||
847 | else | ||
848 | return str | ||
849 | end | ||
850 | return "" | ||
702 | end | 851 | end |
703 | 852 | ||
704 | function processnovoc(str, rules, scheme) | 853 | function processnovoc(str, rules, scheme) |
@@ -717,7 +866,13 @@ function processnovoc(str, rules, scheme) | |||
717 | str = novoc(str) | 866 | str = novoc(str) |
718 | else end | 867 | else end |
719 | str = unprotectarb(str) | 868 | str = unprotectarb(str) |
720 | return str | 869 | if export_utf == "yes" then |
870 | tofile = "\\txarb{"..str.."}" | ||
871 | tooutfile(tofile) | ||
872 | else | ||
873 | return str | ||
874 | end | ||
875 | return "" | ||
721 | end | 876 | end |
722 | 877 | ||
723 | function processtrans(str, mode, rules, scheme) | 878 | function processtrans(str, mode, rules, scheme) |
@@ -739,8 +894,13 @@ function processtrans(str, mode, rules, scheme) | |||
739 | str = transarabica(str) | 894 | str = transarabica(str) |
740 | end | 895 | end |
741 | str = unprotectarb(str) | 896 | str = unprotectarb(str) |
897 | if export_utf == "yes" then | ||
898 | tofile = str | ||
742 | tooutfile(str) | 899 | tooutfile(str) |
743 | return str | 900 | else |
901 | return str | ||
902 | end | ||
903 | return "" | ||
744 | end | 904 | end |
745 | 905 | ||
746 | function newarbmark(abbr, rtlmk, ltrmk) | 906 | function newarbmark(abbr, rtlmk, ltrmk) |
@@ -773,7 +933,14 @@ function processarbmarks(str) | |||
773 | end | 933 | end |
774 | end | 934 | end |
775 | end | 935 | end |
776 | return str | 936 | -- return str |
937 | if export_utf == "yes" then | ||
938 | tofile = str | ||
939 | tooutfile(str) | ||
940 | else | ||
941 | return str | ||
942 | end | ||
943 | return "" | ||
777 | end | 944 | end |
778 | 945 | ||
779 | function uc(str) | 946 | function uc(str) |