diff options
author | Karl Berry <karl@freefriends.org> | 2011-08-02 00:09:00 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2011-08-02 00:09:00 +0000 |
commit | 6189f60645741ffcb1b6e730325243882d7e5526 (patch) | |
tree | 62dfcc1190bd97244ece6865ae07c33d55d13e7f /Master | |
parent | 8cb09a6490220c7e2df1022ea66743b9afa234be (diff) |
new luatex package interpreter (9jul11)
git-svn-id: svn://tug.org/texlive/trunk@23308 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master')
-rw-r--r-- | Master/texmf-dist/doc/luatex/interpreter/README | 26 | ||||
-rw-r--r-- | Master/texmf-dist/doc/luatex/interpreter/interpreter-doc.pdf | bin | 0 -> 72802 bytes | |||
-rw-r--r-- | Master/texmf-dist/doc/luatex/interpreter/interpreter-doc.tex | 98 | ||||
-rw-r--r-- | Master/texmf-dist/doc/luatex/interpreter/interpreter-doc.txt | 383 | ||||
-rwxr-xr-x | Master/texmf-dist/scripts/interpreter/i-doc.lua | 253 | ||||
-rwxr-xr-x | Master/texmf-dist/scripts/interpreter/interpreter.lua | 379 | ||||
-rw-r--r-- | Master/texmf-dist/tex/luatex/interpreter/interpreter.sty | 17 | ||||
-rw-r--r-- | Master/texmf-dist/tex/luatex/interpreter/interpreter.tex | 36 | ||||
-rwxr-xr-x | Master/tlpkg/bin/tlpkg-ctan-check | 2 | ||||
-rwxr-xr-x | Master/tlpkg/libexec/ctan2tds | 2 | ||||
-rw-r--r-- | Master/tlpkg/tlpsrc/collection-luatex.tlpsrc | 1 | ||||
-rw-r--r-- | Master/tlpkg/tlpsrc/interpreter.tlpsrc | 0 |
12 files changed, 1196 insertions, 1 deletions
diff --git a/Master/texmf-dist/doc/luatex/interpreter/README b/Master/texmf-dist/doc/luatex/interpreter/README new file mode 100644 index 00000000000..c62c78604ae --- /dev/null +++ b/Master/texmf-dist/doc/luatex/interpreter/README @@ -0,0 +1,26 @@ +This is the README file for the Interpreter package. +Author: Paul Isambert. +E-mail: zappathustra AT free DOT fr +Comments and suggestions are welcome. +Date: July 2011. +Version: 1.0. + +Interpreter preprocesses input files on the fly (no external program) and +manipulates input lines, e.g. to turn some markup into proper TeX syntax. +LuaTeX is required. + +Relevant information can be found in interpreter-doc.pdf +or interpreter-doc.txt (source of the doc readable in a text editor). + +The files in this distribution are: + +interpreter.lua - main code +interpreter.tex - \input in plain TeX +interpreter.sty - wrapper for LaTeX +interpreter-doc.tex - master file for the doc +interpreter-doc.txt - text of the doc +interpreter-doc.pdf - typeset doc +i-doc.lua - interpretation for the doc (because, of course, the doc + is typeset with Interpreter) + +Licensing of this package is covered by LPPL. diff --git a/Master/texmf-dist/doc/luatex/interpreter/interpreter-doc.pdf b/Master/texmf-dist/doc/luatex/interpreter/interpreter-doc.pdf Binary files differnew file mode 100644 index 00000000000..8e70e34b7fd --- /dev/null +++ b/Master/texmf-dist/doc/luatex/interpreter/interpreter-doc.pdf diff --git a/Master/texmf-dist/doc/luatex/interpreter/interpreter-doc.tex b/Master/texmf-dist/doc/luatex/interpreter/interpreter-doc.tex new file mode 100644 index 00000000000..8d78ad02963 --- /dev/null +++ b/Master/texmf-dist/doc/luatex/interpreter/interpreter-doc.tex @@ -0,0 +1,98 @@ +% This is the master file producing interpreter-doc.pdf. The version of the +% documentation readable in a text editor is interpreter-doc.txt (input below). +% +% Paul Isambert - zappathustra AT free DOT fr - July 2011 +\input pitex + +% Output stuff. +\gates remove {output_headers}{output_shipout} +\gates remove {output_postship}{output_shipout} +\gates remove {output_precheck}{output} +\gates close {output_shipout}{output} + +\newif\ifright +\newbox\leftbox +\gates def {twocol}{% + \ifright + \global\rightfalse + \setbox\outputbox=\hbox{\box\leftbox\kern24pt\box\outputbox}% + \gates ajar {output_shipout}{output}% + \else + \global\righttrue + \global\setbox\leftbox=\box\outputbox + \fi + } +\gates add {twocol}[before output_shipout]{output} + + +\setparameter page : + hsize = 20pc + left = 60pt + width = "\dimexpr 144pt + 40pc\relax" + lines = 45 + height = 24cm + +\setparameter section : + font = \bf + link = true + number = none + numbercommand = \llap + beforeskip = 1 + +\setparameter navigator : + open = true + title = "Interpreter documentation" + author = "Paul Isambert" + mode = outlines + +\setparameter font : + command = \mainfont + name = "Chaparral Pro" + bold = Semibold + big = 18pt + +\setparameter font : + command = \codefont + name = "Lucida Console" + bold = none + size = 8pt + features = "space = .6, -tlig, -trep, -liga" + + +\parfillskip=0pt plus 1fill +\def\describe#1#2#3{% + \iflines3{\vskip\baselineskip}{\breakpage}% + \noindent\color{.8 0 0}{% + {\outline{#3}{\directlua{% + local t = string.gsub("\luaescapestring{#1}", "[ (].*", "") + tex.print(t)}}% + \codefont#1}% + \reverse\iffemptystring{#2} + {\kern1em \hfil\penalty0\hbox{\ital{(#2)}}}}% + \par\removenextindent} + +\newverbatim\source{} + {\vskip\baselineskip + \hfuzz=1em + \codefont\parindent=0pt + \pdfcolorstack0 push {.8 0 0 rg} + \printverbatim + \pdfcolorstack0 pop + \vskip\baselineskip} + +\def\arg#1{{\codefont\char"2039 #1\char"203A}} + +\input interpreter + +% Title +\vbox to 3\baselineskip{ +\hbox to \hsize{\big Interpreter\hfil\normalsize Paul Isambert} +\hbox to \hsize{v.1.0, July 2011 \hfil \tcode{zappathustra AT free DOT fr}} +\vfil +} +% Bulk of the doc. +\interpretfile{doc}{interpreter-doc.txt} +\vskip0pt plus 1filll +\noindent\ital{Typeset with Lua\TeX\ 0.71 in Chaparral Pro and Lucida Console} + +\bye diff --git a/Master/texmf-dist/doc/luatex/interpreter/interpreter-doc.txt b/Master/texmf-dist/doc/luatex/interpreter/interpreter-doc.txt new file mode 100644 index 00000000000..5bece3bac7a --- /dev/null +++ b/Master/texmf-dist/doc/luatex/interpreter/interpreter-doc.txt @@ -0,0 +1,383 @@ + TABLE OF CONTENTS + (Make a search on the tag in the right + column to jump to the associated section. + Vim users can simply type * on the tag; + Emacs users do that with C-s C-w (I think); + Other editors: I don't know!) +======================================= + Introduction intro_tag + Input files input_tag + Paragraphs paragraphs_tag + Declaring patterns patterns_tag + Classes classes_tag + Protecting input protect_tag + Technical stuff technical_tag + An example: i-doc.lua example_tag +======================================= + + + +================================== intro_tag +=== Introduction ================= +================================== + +Interpreter preprocesses input files before their contents is fed to TeX. It +is meant to write document with whatever markup one wishes to define while +using normal TeX macros in the background. As a simple example, suppose you +have a macro "\bold" to put text in boldface; then Interpreter lets you map +"*text*", or "<strong>text</strong>", or simply "!text", or anything else, to +"\bold{text}". Interpreter doesn't perform any trickery with active +characters; instead, it manipulates the strings representing the lines of +a file and search for patterns. + +There are two main advantages: first, TeX documents can be typeset with +a completely non-TeX syntax; second, if one uses some lightweight markup +language, the source file is much easier to read and might even be more useful +than the typeset PDF file, e.g. for some technical documentation you want to +read directly in your text editor while writing code (powerful editors +generally have their own documentation in such a format, for a good reason). +A third advantage, not explored in this documentation, is that while feeding +modified lines to TeX you can also translate the original lines into, say, +HTML, and write them to an external file, thus creating both PDF and HTML +output at once. + + + +================================== input_tag +=== Input files ================== +================================== + +Once Interpreter is loaded with + + \input interpreter + +in plain TeX or + + \usepackage{interpreter} + +in LaTeX, files to be processed are input as follows: + + \interpretfile{<language>}{<file>} + +There should exist a file "i-<language>.lua" containing the language used in +<file>. For instance, the source of this documentation is +"interpreter-doc.txt", input in the master file "interpreter-doc.tex" with + + \interpretfile{doc}{interpreter-doc.txt} + +and the interpretation to be used is defined in "i-doc.lua". The contents of +such an interpretation file is the object of the rest of this documentation. + + + +================================== paragraphs_tag +=== Paragraphs =================== +================================== + +Interpreter doesn't process lines one by one. Instead, it gathers an entire +paragraph and then processes the lines. It is important because you can +manipulate an entire paragraph when a given pattern is detected, and modify +several lines according to what happens in only one. A paragraph in +Interpreter has nothing to do with what TeX considers a paragraph; instead, it +is defined by the following string. + +> interpreter.paragraph [Default: blank line with spaces ignored] + A string to be interpreted as a paragraph boundary when Interpreter collects + lines before processing them. The string actually represents a pattern, so + magic characters are obeyed. The default is "%s*", i.e. a blank line is + considered a paragraph boundary, spaces notwithstanding. Of course, the end + of the file itself is a paragraph boundary. + + + +================================== patterns_tag +=== Declaring patterns =========== +================================== + +Once the lines of a paragraph have been collected, Interpreter searches them +trying to match declared patterns, but it doesn't do so indiscriminately: +patterns are searched in a given order, as explained below. + +Patterns are searched for in each line only, i.e. no match can occur across +lines. However, since you can manipulate entire paragraphs based on a match in +one line, the limitation easily vanishes. + +> interpreter.add_pattern(<table>) + This is the basic function used to defined patterns. The <table> may + contain the following entries, along other entries Interpreter won't use + but which can be useful to you, especially with "call" below. The function + returns a table. + +>> class [Default: "intepreter.default_class"] + The class of the pattern. See the section on classes. + +>> pattern + The pattern to match. Lua's magic characters are in force and should be + escaped with "%" if necessary, unless "nomagic" is "true" (or the pattern + itself is the result of "interpreter.nomagic"). + +>> nomagic [Default: "false"] + A boolean deciding whether the pattern should be transformed with + "interpreter.nomagic". + +>> replace + The replacement for the pattern, applied only if there is no "call" entry. + This may be a string, a table or a function. Interpreter simply executes + something similar to "string.gsub()", hence the replacement follows this + function's ordinary syntax. More precisely, if "replace" is a string, the + pattern is replaced with it; in this string, "%n" may be used to denote the + _n_th capture in the pattern. If "replace" is a table, the first capture or + the entire match (if there is no capture) is used as the key, and the + associated value is used as the replacement. If "replace" is a function, it + is called with the captures passed as arguments, or the entire match if + there is no capture. For instance, the following pattern will replace all + "*text*" with "\bold{text}": + + interpreter.add_pattern{ + pattern = "%*(.-)%*", + replace = [[\bold{%1}]] + } + +>> offset [Default: 0] + The number of positions Interpreter should shift to the right after a match + has occurred. Normally, Interpreter starts searching for another occurrence + of the current pattern at the same position where it found the last one. + However, loops might easily occur: the replacement for a pattern may very + well contain another match for the same pattern, so Interpreter will get + stuck. Suppose for instance you want to replace "TeX" with "\TeX". The + first match will do that, but then Interpreter will start searching again + at the backslash, producing "\\TeX", then "\\\TeX", etc. In this case, if + you set "offset" to 2 in the pattern, then search will start again at the + "e" and no new match will occur. + +>> call + This entry shall contain a function to be called if there is a match (if + this entry exists, "replace" isn't applied). It is meant to perform complex + tasks that aren't amenable to simple string replacement. The function will + be executed as follows: + + function (paragraph, line, index, pattern) + + "paragraph" is a table representing the current paragraph; lines are stored + at successive indices. The last line of this paragraph is always the + paragraph boundary (see "interpreter.paragraph"), unless the paragraph + stopped at the end of the file. The second argument, "line", is a number + representing the index in "paragraph" containing the line where the pattern + was found; "index" is the position in this line where the match occurred. + Finally, "pattern" is the entire table declared with + "interpreter.add_pattern" and containing all the entries discussed here. + + The function may return zero, one, or two numbers. If it returns none, the + search for the next occurrence of the pattern will start again on the same + line (rather, on the line with the same position in the paragraph), at + "index". If it returns one number, the search will resume at the same line + but at position _n_, with _n_ the returned number. Finally, if two numbers + are returned, the search will resume at line _m_ at position _n_, _m_ and + _n_ being the returned values. Specifying which line should be examined + when the search resumes might be necessary if the function adds new lines + in the paragraph _before_ the current line, since Interpreter only keeps + count of line numbers. + + The entire paragraph can thus be modified if necessary. For instance, + suppose you want to declare comments in your source file with only + "!Comment" in the first line, i.e. TeX should ignore a paragraph such as: + + !Comment + This should be ignored + by TeX + + Then the following pattern will do (where the function requires only the + first argument): + + local function comment (paragraph) + for n, l in ipairs(paragraph) do + paragraph[n] = "%" .. l + end + end + interpreter.add_pattern{ + pattern = "^!Comment", + call = comment + } + +> interpreter.nomagic (string) + A function which reverses the usual Lua magic for patterns: ordinary magic + characters are normal characters here, unless they are prefixed with "%", in + which case they are magic again. For instance, a pattern like ".+" is + normally interpreted as ``one or more characters''. If passed to this + function, a pattern is returned meaning ``a dot followed by a plus sign''. + On the contrary, "%.%+" normally has the second interpretation, while with + "interpreter.nomagic" it has the first one. The function makes another + transformation: "..." is used to denote a capture "(.-)". Thus + "interpreter.nomagic('*...*')" returns a pattern matching any number of + characters surrounded by stars and capturing those characters; this would be + expressed in ordinary Lua magic as "%*(.-)%*". + + + +================================== classes_tag +=== Classes ====================== +================================== + +As already alluded to, the search for patterns isn't done at random. Instead, +patterns are organized in classes, which are applied one after the other. More +precisely, the process is as follows: Interpreter searches the entire +paragraph for the first pattern in class~1, then for the second pattern in the +same class, then for the third, etc., then when there is no pattern left in +class~1 it does the same with class~2, up to class~_n_, where _n_ is the +hightest class number such that there exists a class _n - 1_ (in other words, +classes should be numbered consecutively). Finally, the same goes for the +patterns in class~0 (which always exists, even if it contains no pattern). + +Inside a class, patterns are ordered by length from long to short, or +alphabetically if two patterns have the same length. This means that if you +use e.g. "/text/" for italics and "//text//" for bold, you don't need to put +the second pattern in a class before the first to avoid "//text//" being +interpreted as two empty arguments in italics surrounding a text in roman. +Since the way the bold-pattern will be declared, e.g. "//(.-)//", is probably +longer than for the italic-pattern, e.g. "/(.-)/", it will always match first. + +That said, the sorting isn't very clever and simply relies on the number of +symbols, no matter what they mean; in the patterns above, the parentheses +denote a capture but they still count in the pattern's length as understood by +Interpreter. Alternatively, while ".*" denotes ``zero or more character'' and +"%+" means ``a plus sign'' ("+" being magic, you have to escape it to refer to +it), in Interpreter's eye the two patterns have the same length: two. Finally, +one should be aware that patterns declared with a "nomagic" entry set to +"true" are sorted after they've been transformed (so that their real length +might not be obvious). So classes are needed when patterns need a proper +ordering no matter their lengths. For instance, some patterns should always be +declared first, as they protect input from Interpreter (see next section), +while others might need to be declared last, as they rely on what previous +patterns might have done. Besides, classes are metatables for the patterns +they contain. + +> interpreter.default_class [Default: 1] + All patterns belong to a class, even though you may omit the "class" entry + when declaring one. In this case, the pattern is assigned to the class + denoted by this number. + +> interpreter.set_class(number, table) + Defines class "number" as "table". Classes don't need to be defined + beforehand for patterns to be added to them (rather, Interpreter defines + them implicitly when needed). However, classes are also metatables for the + patterns, so that if there lacks an entry in a pattern's table, the class's + entry is used if it exists. The function returns a table. + + + +================================== protect_tag +=== Protecting input ============= +================================== + +Sometimes you want Interpreter to refrain from interpreting; that is most +useful for verbatim code, for instance. There are various ways to do that. + +> interpreter.active [Default: true] + A boolean switching Interpreter on and off. Beware, the switching applies + only starting at the next paragraph. + +> interpreter.protect([line]) + A function protecting all or part of the current paragraph. If "line" is + given, it should be a number _n_, and line _n_ in the current paragraph will + be protected; without "line", the entire paragraph is protected. Protecting + means that the patterns not yet searched for will be ignored. For instance, + if you want material to be read verbatim when surrounded with "<code>" and + "</code>", you can declare a pattern as follows: + + local function verbatim (buffer) + buffer[1] = "\\verbatim" + buffer[#buffer - 1] = "\\endverbatim" + intepreter.protect() + end + interpreter.add_pattern{ + pattern = "^%s*<code>%*s$", + call = verbatim, + class = 1 + } + + This code is extremely simplified : it assumes that "<code>" and "</code>" + starts and ends the paragraph and that "</code>" isn't the last line of the + file (otherwise it'd also be the last line in the paragraph, whereas here + the last one is the paragraph boundary). An important point is that the + pattern belongs to the first class, so it is called before all other + patterns (provided there is no shorter pattern in class~1) and prevents them + from doing anything, since the entire paragraph is protected. (Typesetting + the material as verbatim material obviously depends on the "\verbatim" + macro, not on Interpreter.) + +> interpreter.escape + A character which prevents patterns from being replaced if immediately + preceded by it. As an example, if "interpreter.escape = '_'", and "*text*" + denotes italic, then "*text*" will produce _text_ while "_*text*" will + produce *text*. Once a paragraph has been processed, Interpreter removes all + escape characters. Only one character can be an escape character. + +> interpreter.protector(left[, right]) ["right" defaults to "left"] + Defines two characters to protect what they surround. In other words, + Interpreter replaces patterns only if the match isn't found between "left" + and "right". Unlike the escape character, you can define as many protectors + as you wish; and unlike the escape character again, Interpreter _doesn't_ + remove them once the paragraph has been processed, so you must take care of + them. For instance: + + intepreter.protector('"') + interpreter.add_pattern{ + pattern = '"(.-)"', + replace = '\\verb`%1`', + class = 0 + } + + Anything between double quotes will be left untouched; then, when the + paragraph has been processed for all other classes, a pattern in class~0 + calls the "\verb" command to take care of the argument. Note that the + protectors should enclose what they protect without coinciding with it; this + is not the case here, which is why the pattern is applied. + +> interpreter.direct [Default: two percent signs then "I" and at least one space] + A string, actually a pattern, signalling that the line which it begins + should be processed as Lua code. The default is "%%%%I%s+", i.e. "%%I" + followed by at least one space. The pattern shouldn't declare itself as + attached to the beginning of the line (as in "^%%%%I%s+") because they will + be matched at the beginning of the line only anyway. The line is processed + with the "loadstring" function, and then turned into an empty line. For + instance: + + %%I interpreter.active = false + This won't be interpreted... + %%I interpreter.active = true + + As this example shows, lines flagged with "interpreter.direct" don't obey + "interpreter.active" and are always processed as described above. + + + +================================== technical_tag +=== Technical stuff ============== +================================== + +You don't have to bother with this section if you don't mind how Interpreter +does its job; actually you won't learn much anyway. + +> interpreter.reset() + A function which resets everything to default and deletes classes. It is used + when calling "\interpretefile" so that new interpretetions start from zero. + +> interpreter.register(function) + A function called to put Interpreter's main function into the + "post_linebreak_filter" callback; you can redefine it at will. If it is + undefined, "callback.register()" is used, unless "luatexbase.add_to_callback()" + is detected. (The detection takes place at the first call to + "\interpretfile", so there is no need to load Interpreter after + "luatexbase".) + +> interpreter.unregister(function) + A function called to remove Interpreter's main function from the + "post_linebreak_filter" callback. It works similarly to the previous one. + + + +================================== example_tag +=== An example: i-doc.lua ======== +================================== + +\interpretfile{doc}{i-doc.lua} diff --git a/Master/texmf-dist/scripts/interpreter/i-doc.lua b/Master/texmf-dist/scripts/interpreter/i-doc.lua new file mode 100755 index 00000000000..b8b3c106a46 --- /dev/null +++ b/Master/texmf-dist/scripts/interpreter/i-doc.lua @@ -0,0 +1,253 @@ +--[[ +Here's a description of "i-doc.lua", the file containing the interpretation +used for Interpreter's documentation. Remember that none of the TeX macros +used here is defined by Interpreter; instead, they are my own and should be +adapted if necessary. Also several options taken here are far from optimal but +are convenient examples. + +Shorthands for often used functions. +--]] +local gsub, match = string.gsub, string.match +local add_pattern = interpreter.add_pattern +local nomagic = interpreter.nomagic + +--[[ +Class 1 and 2 will be used for verbatim (thus protecting) and ``normal'' +patterns go into class 3 or higher. +--]] +interpreter.default_class = 3 + +--[[ +The reader might have observed that "interpreter-doc.txt" begins with a table +of contents. This table is useful for the source file only, and isn't typeset +by TeX, because the following pattern suppresses it: the entire paragraph +containing "TABLE OF CONTENTS" on a line of its own is deleted. Protecting the +paragraph is useless, but it makes things a little bit faster because the +paragraph won't be pointlessly searched for other patterns. +--]] +local function contents (buffer) + for n in ipairs(buffer) do + buffer[n] = "" + end + interpreter.protect() +end +add_pattern{ + pattern = "^%s*TABLE OF CONTENTS%s*$", + call = contents, + class = 1 +} + +--[[ +Sections headers are typeset as + + ====================================== section_tag + === Section title ==================== + ====================================== + +The first and third line are decorations and they are removed. The +"section_tag" is meant for the source only again (linking the section to the table +of contents). I could have used it to create PDF destinations, but that seemed +unnecessary in such a small file. The associated pattern is: at least four +equals signs. +--]] +add_pattern{ + pattern = "^====+.*", + replace = "" +} + +--[[ +The middle line is spotted with the tree equals sign at the beginning of the +line (the previous pattern being longer, the decoration lines have been +already removed and they won't be taken for section titles). The signs are +removed and replaced with "\section{" and "}". +--]] +local function section (buffer, num) + local l = buffer[num] + l = gsub(l, "^===%s*", "\\section{") + l = gsub(l, "%s*=+%s*", "}") + buffer[num] = l +end +add_pattern{ + pattern = "^===", + call = section +} + +--[[ +The following pattern simply turns "Interpreter" into "\ital{Interpreter}". The +meaning of the "\ital" command is obvious, I suppose. Note the offset: +starting at the backslash, this leads to the _n_ in Interpreter, thus avoiding +matching the pattern again. The Lua notation with double square brackets is +used for strings with no escape character (hence "\ital" and not "\\ital" as +would be necessary with a simple string). +--]] +add_pattern{ + pattern = "Interpreter", + replace = [[\ital{Interpreter}]], + offset = 7 +} + +--[[ +Turning "TeX" into TeX. This illustrates the use of a function as "replace"; +the point is that "\TeX" should be suffixed with a space if initially followed +by anything but a space or end of line (so as not to form a control sequence +with the following letters), and it should be suffixed with a control space if +initially followed by a space or end of line (so as to avoid gobbling the +space). So the function checks the second capture. Note that simply replacing +"TeX" with "\TeX{}" would be much simpler, but less instructive! +--]] +local function maketex (tex, next) + if next == " " or next == "" then + return [[\TeX\ ]] + else + return [[\TeX ]] .. next + end +end +add_pattern{ + pattern = "(TeX)(.?)", + replace = maketex, + offset = 2 +} + +--[[ +The following turns "<text>" into <text> and "_text_" into _text_. Setting a +class just so the patterns inherit the "nomagic" feature is of course an +overkill, but that's an example. +--]] +interpreter.set_class(4, {nomagic = true}) +add_pattern{ + pattern = "<...>", + replace = [[\arg{%1}]], + class = 4 +} +add_pattern{ + pattern = "_..._", + replace = [[\ital{%1}]], + class = 4 } + +--[[ +I use double quotes as protectors; they are replaced with a "\verb" command at +the very end of the processing (with class 0). +--]] +interpreter.protector('"') +add_pattern{ + pattern = nomagic'"..."', + replace = [[\verb`%1`]], + class = 0 +} + +--[[ +The description of functions (in red in the PDF file) are handled with the +"\describe" macro, which takes the function as its first argument and +additional information as its second one (typeset in italics in the PDF file). +In the source, it is simply marked as + + > function (arguments) [Additional information] + +with "[Additional information]" sometimes missing (i.e. there is no empty +pairs of square brackets). Descriptions of entries in pattern tables follows +the same syntax, except the line begins with ">>". So the pattern first spots +lines beginning with ">[>]" followed by at least one space, adds an empty pair of +brackets at the end if there isn't any, and turn the whole into "\describe". +The number of ">" symbols sets "\describe"'s third argument, which specifies +the level of the bookmark. +--]] +local function describe (buffer, num) + local l = buffer[num] + if not match (l, "%[.-%]%s*$") then + l = l .. " []" + end + local le = match(l, ">>") and 4 or 3 + buffer[num] = gsub(l, ">+%s+(.-)%s+%[(.-)%]", + [[\describe{%1}{%2}{]] .. le .. "}") +end +add_pattern{ + pattern = "^>+%s+", + call = describe +} + +--[[ +Here's how multiline verbatim is handled; in the source it is simply marked by +indenting the line with ten spaces; thus code is easily spotted when reading +the source without useless and annoying "<code>"/"</code>" or anything similar +to mark it. To be properly processed by TeX, the code should be surrounded by +"\verbatim" and "\verbatim/" (my way of signalling blocks). Those must be on +their own lines, so we insert a line at the beginning and at the end of the +paragraph: for the closing "\verbatim/", we can simply replace the last line +of the paragraph, which is the boundary line, unless we're at the end of the +file. But for the opening "\verbatim" a line must be added at the beginning of +the paragraph; thus line numbers in the original source file and in its +processed version don't match anymore, and this might be annoying when TeX +reports erros. Besides, blank verbatim lines aren't handled correctly and +create a new verbatim block instead. So this way of marking verbatim material +is good for small documents, but explicit marking is cleaner and more +powerful (albeit not so good-looking in the source file). + +Note that the verbatim pattern belongs to class 2 and the entire paragraph is +protected, so Interpreter leaves it alone afterward (remember the default +class is 3). Of course, the first ten space characters are removed. +--]] +local function verbatim (buffer) + for n, l in ipairs(buffer) do + buffer[n] = gsub(l, "%s%s%s%s%s%s%s%s%s%s","") + end + table.insert(buffer, 1, [[\verbatim]]) + if gsub(buffer[#buffer], + interpreter.paragraph, "") == "" then + buffer[#buffer] = [[\verbatim/]] + else + table.insert(buffer, [[\verbatim/]]) + end + interpreter.protect() +end +add_pattern{ + pattern = "^%s%s%s%s%s%s%s%s%s%s", + call = verbatim, + class = 2 +} + +--[[ +And now comes the fun part. I wanted "i-doc.lua" to be self-describing. The +source of what you're reading right now isn't "interpreter-doc.txt", but +"i-doc.lua" itself input in the latter file with + + \intepreterfile{doc}{i-doc.lua} + +How should code and comment be organized in "i-doc.lua"? Well, there is little +choice, since the file is a normal Lua file: comment lines should be prefixed +with "--" or surrounded with +\tcode{--[{}[} and \tcode{--]{}]}. % Sorry for the braces, I can't nest Lua comments! +I chose the latter option, which is simpler. But normal code should also be +typeset as verbatim material; I could have begun all lines with ten spaces, +but that would have seemed strange. Instead, \tcode{--]{}]} is turned into +"\source" and "\source/" is added at the end of the paragraph ("\source" is +just "\verbatim" with a different layout). Which means all paragraphs have the +same structure: comments between +\tcode{--[{}[} and \tcode{--]{}]} +and code immediately following (\tcode{--[{}[} is simply removed). The pattern +is in class 1 and the paragraph is protected, so that lines indented with ten +spaces or more aren't touched by the previous verbatim pattern (in class 2). +--]] +local function autoverbatim (buffer, line) + buffer[line] = [[\source]] + for n = line + 1, #buffer do + interpreter.protect(n) + end + if gsub(buffer[#buffer], + interpreter.paragraph, "") == "" then + buffer[#buffer] = [[\source/]] + else + table.insert(buffer, [[\source/]]) + end +end +add_pattern{ + pattern = nomagic"%^--]]", + call = autoverbatim, + class = 1 +} +local function test () + return "" +end +add_pattern{ + pattern = nomagic"%^--[[", + replace = test, +} diff --git a/Master/texmf-dist/scripts/interpreter/interpreter.lua b/Master/texmf-dist/scripts/interpreter/interpreter.lua new file mode 100755 index 00000000000..3c2d3391eec --- /dev/null +++ b/Master/texmf-dist/scripts/interpreter/interpreter.lua @@ -0,0 +1,379 @@ +-- This is the main Lua file for the Interpreter package. +-- Further information in interpreter-doc.pdf or interpreter-doc.txt. +-- Paul Isambert - zappathustra AT free DOT fr - July 2011 + +local find, gsub, match, sub = string.find, string.gsub, string.match, string.sub +local insert, sort = table.insert, table.sort + +-- Utility function sorting patterns by length (alphabetically if they are of +-- equal length). +local function string_order (a, b) + local a, b = a.pattern, b.pattern + return #a == #b and a < b or #a > #b +end + +interpreter = { +-- *** interpreter.active *** +-- Following paragraphs (as defined by interpreter.paragraph) are interpreted +-- iff this is not set to false. + active = true, +-- *** interpreter.default_class *** +-- Sets the default class for patterns which are added without specifying the +-- class. Default 1. + default_class = 1 + } +local _classes = {} + +-- *** interpreter.add_pattern (table) *** +-- Creates pattern <table>, which can contain the following entries: +-- pattern [string] = The pattern to match. Magic characters are obeyed! +-- replace [string] = The replacement for <pattern>. Can be a string, a +-- table or a function. A simple string.gsub() is +-- applied. +-- call [function] = The function applied to <pattern>; <replace> is applied +-- iff there is no <call>. +-- offset [number] = If <pattern> is used at index n, then the search on the +-- same line for the same pattern starts again at index n +-- + offset. Applied only when no <call> (in this case, +-- search starts again at the beginning of the line). By +-- default, offset = 0. This is needed to avoid infinite +-- loops with replacements which contain the pattern; +-- e.g. replacing "TeX" with "\TeX" will produce an +-- infinite loop, unless offset = 2. +-- nomagic [boolean] = Sets whether <replace> should be transformed with interpreter.nomagic. +-- class [number] = The pattern's <class> (classes of patterns are applied in +-- order, e.g. all patterns in class 1 are applied, then +-- all patterns in class 2, etc; class 0, however, is +-- always applied last). If <class> is not given, the +-- default_class number is used. Classes must be numbered +-- consecutively. +function interpreter.add_pattern (tb) + local class = tb.class or interpreter.default_class + interpreter.set_class(class, {}) + setmetatable(tb, _classes[class].meta) + if tb.nomagic then + tb.pattern = interpreter.nomagic(tb.pattern) + end + insert(_classes[class], tb) + sort(_classes[class], string_order) + return tb +end + +-- *** interpreter.set_class (number, table) *** +-- Sets default values (of the table normally specified in add_pattern) for +-- patterns of class <number>; patterns added to this class can still specify +-- different values, which will override defaults. In other words, this is a +-- metatable for patterns (which are tables) of that class. +function interpreter.set_class(num, tb) + _classes[num] = _classes[num] or { meta = { __index = function (_, k) return _classes[num].meta[k] end } } + for a, b in pairs(tb) do + _classes[num].meta[a] = b + end + return _classes[num] +end + +-- Class 0 must exist since it is always used at the end of the paragraph. +interpreter.set_class(0, {}) + +-- *** interpreter.nomagic (string) *** +-- Turns a normal string into a string with magic characters escaped, so it +-- can be used as a pattern. +local magic_characters = { + ["^"] = "%^", + ["$"] = "%$", + ["("] = "%(", + [")"] = "%)", + ["%"] = "%%", + ["."] = "%.", + ["["] = "%[", + ["]"] = "%]", + ["*"] = "%*", + ["+"] = "%+", + ["-"] = "%-", + ["?"] = "%?", +} +--function interpreter.nomagic (str) +-- return gsub(str, ".", magic_characters) +--end +function interpreter.nomagic (str) + local i, s = 1, "" + while i <= #str do + local c, c2, c3 = sub(str, i, i), sub(str, i + 1, i + 1), sub(str, i + 2, i + 2) + i = i + 1 + if c == "%" and magic_characters[c2] then + s = s .. c2 + i = i + 1 + elseif c == "." and c2 == "." and c3 == "." then + s = s .. "(.-)" + i = i + 2 + elseif magic_characters[c] then + s = s .. "%" .. c + else + s = s .. c + end + end + return s +end +-- *** interpreter.protect ([spec]) *** +-- Protects a set of lines in a paragraph; a protected line won't be +-- interpreted. If <spec> is a number, this protects line <spec> in the current +-- paragraph; if <spec> is true, this protects the entire current paragraph. Of +-- course, patterns that were applied to the line(s) or paragraph before +-- protection happened aren't undone. +local _protected +function interpreter.protect (num) + if type(num) == "number" then + if type(_protected) ~= "boolean" then + _protected = _protected or {} + _protected[num] = true + end + else + _protected = true + end +end + +-- Utility function making a replacement in a string but only from a certain +-- position and only once. We can't let gsub unrestricted, because some +-- part(s) of the string might be protected. +local function xsub (str, num, patt, rep) + return sub(str, 1, num-1) .. gsub(sub(str, num), patt, rep, 1) +end + +-- *** interpreter.protector (left [, right]) *** +-- Sets <left> and <right> (set to <left> if missing) as protectors, i.e. +-- enclosed material won't be processed even if the line is processed +-- otherwise. For instance: after interpreter.protector ("|"), the word +-- "little" in +-- +-- Hello, |little| world! +-- +-- will be left untouched; Interpreter is terribly smart (thanks to lpeg), so +-- in "|a| b |c|", "b" isn't protected, as intended, because the "|" on its +-- left doesn't match the one on its right but with the one before "a". An +-- example with <right> specified: interpreter.protector("[", "]") and +-- then: +-- +-- Hello, [little] world! +-- +-- achieves the same as above. Protectors AREN'T removed when the line is +-- finally passed to TeX; and there can be several protectors. Compare with +-- interpreter.escape. +local P, Cf, Cg, Cp, Ct, V = lpeg.P, lpeg.Cf, lpeg.Cg, lpeg.Cp, lpeg.Ct, lpeg.V +local _grammar +local function _protector (str, index) + local protections = Cf(Ct("") * Cg{ _grammar + 1 * V(1) }^1, rawset) + protections = protections:match(str) + if protections then + for a, b in pairs(protections) do + if index > a and index < b then + return nil, b + end + end + end + return index +end +function interpreter.protector (left, right) + right = right or left + local gram = P(Cp() * P(left) * (1 - P(right))^0 * Cp() * P(right)) + if _grammar then + _grammar = _grammar + gram + else + _grammar = gram + end +end + +-- *** interpreter.escape *** +-- A string used as an escape character: if a pattern matches, it is processed +-- iff the character immediately to its left isn't <escape>. The escape +-- character IS removed once the lines have been processed, so TeX never sees +-- it; also, only one escape character is allowed, and itself can't be escaped +-- (i.e. it doesn't mean anything to try to escape it). E.g.: +-- +-- interpreter.escape = "|" +-- ... this won't be |*processed* +-- +-- Assuming you have a pattern with stars, here it won't be applied. Instead +-- "this won't be *processed*" will be passed to TeX (note that the escape +-- character has disappeared). + +local function get_index (str, patt, index) + index = find(str, patt, index) + if index then + if sub(str, index-1, index-1) == interpreter.escape then + return get_index(str, patt, index + 1) + elseif _grammar then + local right + index, right = _protector(str, index, patt) + return index or get_index(str, patt, right + 1) + else + return index + end + end +end + +-- The main function that applies all the patterns in a <class> on a given +-- paragraph. +local function process_buffer (buffer, class) + for _, tb in ipairs(class) do + local num, idx = 1, 1 + while num <= #buffer do + local line = type(buffer[num]) == "string" and buffer[num] + if line then + local index = get_index(line, tb.pattern, idx) + if index then + if tb.call then + local l, o = tb.call(buffer, num, index, tb) + if o then + num, idx = l, o + elseif l then + idx = l + end + elseif tb.replace then + buffer[num] = xsub(line, index, tb.pattern, tb.replace) + idx = index + (tb.offset or 0) + end + -- If there was a function applied, protection might have been + -- enforced. Acts accordingly. + if _protected then + if type(_protected) == "table" then + for n, _ in pairs(_protected) do + if type(buffer[n]) == "string" then + buffer[n] = {buffer[n]} + end + end + if _protected[num] then + num = num + 1 + end + else + break + end + end + else + num = num + 1 + idx = 1 + end + else + num = num + 1 + idx = 1 + end + end + end +end + +-- The functions that iterates the previous one for each class. Note that +-- class 0 is processed last. +local function read_buffer (buffer) + for _, class in ipairs(_classes) do + if type(_protected) ~= "boolean" then + process_buffer(buffer, class) + end + end + if type(_protected) ~= "boolean" then + process_buffer(buffer, _classes[0]) + end + _protected = nil + -- Changes protected lines (tables) back to strings. + for n, _ in ipairs(buffer) do + if type(buffer[n]) == "table" then + buffer[n] = buffer[n][1] + end + end + return buffer +end + +local lines = { } + +-- *** interpreter.paragraph *** +-- The pattern that defines a line acting as a paragraph boundary, +-- prompting Interpreter to process the lines gathered up to now. Default is a +-- line composed of spaces at most. +interpreter.paragraph = "%s*" + +-- *** interpreter.direct (pattern) *** +-- Sets the pattern defining a line as direct Lua code: if a line begins with +-- <pattern> (which itself shouldn't contain the beginning-of-string character "^") +-- the code that follows is processed as Lua code, and the line is turned to +-- an empty string; note that this empty string will be seen as a paragraph +-- boundary if the line happened in the middle of a paragraph and +-- interpreter.paragraph has set paragraph boundary to empty string. Default +-- is "%%I " (two "%" followed by one "I" followed by at least one space +-- character). +interpreter.direct = "%%%%I%s+" + +-- At last, the function to be registered in open_read_file, defining the +-- function that reads a file. +function interpreter.read_file (fname) + -- *** interpreter.unregister () *** + -- The function used to remove read_file from the "open_read_file" callback. + -- Uses callback.register by default, or luatexbase.remove_from_callback if + -- detected. + if not interpreter.unregister then + if luatexbase and luatexbase.remove_from_callback then + function interpreter.unregister () + luatexbase.remove_from_callback("open_read_file", "interpreter") + end + else + function interpreter.unregister () + callback.register("open_read_file", nil) + end + end + end + interpreter.unregister() + + local f, line = io.open(fname), true + + local read_line = function () + + -- If no lines is left in store, we gather a new paragraph. + if #lines == 0 then + local line = f:read () + while line do + -- Checks for direct code. + if interpreter.direct and match(line, "^" .. interpreter.direct) then + loadstring(gsub(line, "^" .. interpreter.direct, ""))() + line = "" + end + table.insert(lines, line) + -- Checks for paragraph boundary, which breaks line gathering. + if gsub(line, interpreter.paragraph, "") == "" then + break + end + line = f:read () + end + + -- Pass the paragraph (a table of strings) to the bug functions above, + -- if interpreter is active; then remove escape characters, if any. + if interpreter.active then + lines = read_buffer (lines) + if interpreter.escape then + for num, line in ipairs(lines) do + lines[num] = gsub(line, interpreter.escape, "") + end + end + end + end + + return lines and table.remove (lines, 1) + end + return {reader = read_line} +end + +-- *** interpreter.reset () *** +-- Resets to default values; mostly to be used when calling a new +-- interpretation with \interpretfile. +function interpreter.reset () + interpreter.active = true + interpreter.default_class = 1 + _classes = {} + interpreter.set_class(0, {}) + _grammar = nil + interpreter.escape = nil + interpreter.paragraph = "%s*" + interpreter.direct = "%%%%I%s+" +end + +-- *** interpreter.register (function) *** +-- The function used to register the read_file function in the +-- "open_read_file" callback. If none is given, use callback.register, or +-- luatexbase.add_to_callback if detected (with "interpreter" as the name). +-- The function is defined in \interpretfile (see interpreter.tex). diff --git a/Master/texmf-dist/tex/luatex/interpreter/interpreter.sty b/Master/texmf-dist/tex/luatex/interpreter/interpreter.sty new file mode 100644 index 00000000000..9ffcad52f1c --- /dev/null +++ b/Master/texmf-dist/tex/luatex/interpreter/interpreter.sty @@ -0,0 +1,17 @@ +% This is interpreter.sty, a style file to load +% the Interpreter package in LaTeX. Useful information +% can be found in interpreter-doc.pdf or interpreter-doc.txt. +% +% Author: Paul Isambert. +% E-mail: zappathustra AT free DOT fr +% Comments and suggestions are welcome. +% Date: July 2011. + +\NeedsTeXFormat{LaTeX2e} +\ProvidesPackage{interpreter}[2011/08/07 v.1.0 Preprocessing input files on the fly.] +% Needed to prevent LaTeX check input, which would call open_read_file and +% thus remove Interpreter's main function (which is removed as soon as it is +% used). +\expandafter\let\expandafter\interpreterinput\csname @@input\endcsname +\input interpreter.tex +\endinput diff --git a/Master/texmf-dist/tex/luatex/interpreter/interpreter.tex b/Master/texmf-dist/tex/luatex/interpreter/interpreter.tex new file mode 100644 index 00000000000..634c1ef826f --- /dev/null +++ b/Master/texmf-dist/tex/luatex/interpreter/interpreter.tex @@ -0,0 +1,36 @@ +% This is the main TeX file for the Interpreter package. +% Further information in interpreter-doc.pdf or interpreter-doc.txt. +% +% Paul Isambert - zappathustra AT free DOT fr - July 2011 +\csname Oh no, I won't be loaded twice!\endcsname +\expandafter\let\csname Oh no, I won't be loaded twice!\endcsname\endinput +\directlua{require("interpreter.lua")} +\def\interpretergobble#1{} +\unless\ifdefined\interpreterinput + \let\interpreterinput\input +\fi +\def\interpretfile#1#2{% + \directlua{% + local f = kpse.find_file("i-#1.lua") + if f then + interpreter.reset() + dofile(f) + if not interpreter.register then + if luatexbase and luatexbase.add_to_callback then + function interpreter.register (f) + luatexbase.add_to_callback("open_read_file", f, "interpreter") + end + else + function interpreter.register (f) + callback.register("open_read_file", f) + end + end + end + interpreter.register(interpreter.read_file) + tex.print([[\noexpand\interpreterinput]]) + else + tex.error("I can't find file `i-#1.lua'. I won't input file `#2'") + tex.print([[\noexpand\igobble]]) + end + }{#2}% + } diff --git a/Master/tlpkg/bin/tlpkg-ctan-check b/Master/tlpkg/bin/tlpkg-ctan-check index 53d7cb87739..f36ff00c86b 100755 --- a/Master/tlpkg/bin/tlpkg-ctan-check +++ b/Master/tlpkg/bin/tlpkg-ctan-check @@ -187,7 +187,7 @@ my @TLP_working = qw( ijmart ijqc imac image-gallery imakeidx impatient impatient-fr import imtekda inconsolata inlinebib inlinedef inputtrc insbox installfont - interactiveworkbook interfaces intro-scientific inversepath + interactiveworkbook interfaces interpreter intro-scientific inversepath ionumbers iopart-num iso iso10303 isodate isodoc isomath isonums isorot isotope itnumpar iwhdp iwona diff --git a/Master/tlpkg/libexec/ctan2tds b/Master/tlpkg/libexec/ctan2tds index 14969de8d11..5c424f97902 100755 --- a/Master/tlpkg/libexec/ctan2tds +++ b/Master/tlpkg/libexec/ctan2tds @@ -1125,6 +1125,7 @@ $standardtex='\.(.bx|cfg|sty|clo|ldf|cls|def|fd|cmap|4ht)$'; 'ifmslide', '[^n]\.(eps|pdf|jpg)|' . $standardtex, # not ifmman.pdf 'insbox', 'insbox.tex', 'interactiveworkbook', 'NULL', # done in POSTinteractiveworkbook + 'interpreter', 'interpreter.tex|' . $standardtex, 'iso10303', 'a.*tex|b.*tex|e.*tex|stp.*tex|\.sty|\.4ht', 'isodate', '\.idf|\.sty', 'isodoc', 'isodoc.cls', # not mystyle.sty, isontg.sty, etc. @@ -1929,6 +1930,7 @@ $standardxmt='\.xmt'; # scripts to install under texmf*/.../scripts, but not bindir. %specialscripts_nobin = ( 'epspdf' => '(epspdf(|\.help|boot|tk)|\.rb|makegray\.pro)$', + 'interpreter' => '\.lua$', 'lua-alt-getopt' => '\.lua$', 'luaindex' => '\.lua$', 'lualatex-math' => '\.lua$', diff --git a/Master/tlpkg/tlpsrc/collection-luatex.tlpsrc b/Master/tlpkg/tlpsrc/collection-luatex.tlpsrc index 5f8a4517e8f..0154e8d6e93 100644 --- a/Master/tlpkg/tlpsrc/collection-luatex.tlpsrc +++ b/Master/tlpkg/tlpsrc/collection-luatex.tlpsrc @@ -5,6 +5,7 @@ longdesc as an embedded scripting and extension language. http://luatex.org/ # depend collection-basic # +depend interpreter depend luacode depend luainputenc depend lualatex-doc diff --git a/Master/tlpkg/tlpsrc/interpreter.tlpsrc b/Master/tlpkg/tlpsrc/interpreter.tlpsrc new file mode 100644 index 00000000000..e69de29bb2d --- /dev/null +++ b/Master/tlpkg/tlpsrc/interpreter.tlpsrc |