summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/lua-ul/docstrip-luacode.sty
blob: fe5c1b4f7548c80b8e5cfe0413723e10a1dc7c30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
%% Copyright (C) 2020-2021 by Marcel Krueger
%%
%% This file may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either
%% version 1.3c of this license or (at your option) any later
%% version. The latest version of this license is in:
%%
%% http://www.latex-project.org/lppl.txt
%%
%% and version 1.3 or later is part of all distributions of
%% LaTeX version 2005/12/01 or later.

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage
  {docstrip-luacode}
  [2021/04/25 v0.1.3 Directly execute Lua code from DocStrip files]
\expanded{%
  \def\noexpand\docstrip@luacode@argscanner#1\directlua{
  tex.sprint(\the\catcodetable@string, "\string\\end{docstrip-luacode}")
  local scanner = token.create'docstrip@luacode@argscanner'
  local file = lpeg.Ct((lpeg.P'\csstring\%' * (1-lpeg.P'\string\r')^0 + lpeg.C((1-lpeg.P'\string\r')^0) * '\string\r')^0)/function(s)return table.concat(s,'\string\n')end
  local func = luatexbase.new_luafunction'docstrip-luacode'
  token.set_lua('docstrip-luacode', func, 'protected')
  lua.get_functions_table()[func] = function()
    local name = token.scan_string()
    tex.catcodetable = \the\catcodetable@string
    tex.setcatcode(32, 12)
    token.put_next(scanner)
    token.scan_token()
    package.preload[name], msg = loadstring(file:match(token.scan_string()))
  end
}{\relax{#1}\noexpand\end{docstrip-luacode}}}
\endinput