summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/semantex/stripsemantex.sty
blob: eba56386248864412a03dad2ed9f2324fb9bd35b (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
\ProvidesExplPackage{stripsemantex}{2020/09/07}{0.1alpha}{}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%																		%
%		stripSemanTeX -- strip the document of SemanTeX markup			%
%		https://ctan.org/pkg/semantex									%
%		(C) 2020 Sebastian Ørsted										%
%       sorsted@gmail.com												%
%																		%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\msg_new:nnn { stripsemantex } { not_luatex } { This~package~only~works~with~LuaTeX;~the~current~TeX~run~has~been~terminated. }

\msg_new:nnn { stripsemantex } { source_not_expected } { The~source~code~in~the~file~did~not~fit~the~code~in~the~.semtex~file.~We~expected~#1~but~got~#2.~This~is~not~supposed~to~happen.~Please~report~this~bug~ASAP. }

\msg_new:nnn { stripsemantex } { no_semtex_file } { The~file~#1.semtex~was~not~found.~Did~you~remember~to~do \\ \token_to_str:N\SemantexSetup{semtexfile=true}? }

\msg_new:nnn { stripsemantex } { begin_document_not_found } { No~\token_to_str:N\begin{document}~found~in~the~file~#1;~it~has~to~be~in~the~main~document. }

\sys_if_engine_luatex:F { \msg_fatal:nn { stripsemantex } { not_luatex } }

\lua_now:n { require('stripsemantex.lua') }

\cs_new_protected:Npn\stripsemantex_create_prestripped_file:n#1
{	
	\file_if_exist:nF { #1.semtex }
	{
		\msg_fatal:nnn { stripsemantex } { no_semtex_file } { #1 }
	}
	
	\lua_now:e {
		copy_file('\lua_escape:e{#1}.tex', '\lua_escape:e{#1}_prestripped.tex')
		openFile('\lua_escape:e{#1}_prestripped.tex')
	}
	
	\cs_set:Npn\DeclareObject##1
	{
		\lua_now:e { addIDsToRegisters ( '\cs_to_str:N ##1' ) }
	}
	
	\cs_set:Npn\DeclareClass##1{
		\lua_now:e { addIDsToRegisters ( '\cs_to_str:N ##1' ) }
	}
	
	\cs_set:Npn\BeginSource##1\EndSource\BeginOutput##2\EndOutput{}
	
	\file_input:n { #1.semtex }
	
	\lua_now:e
	{
		removeSuperfluousIDs()
		addNumbersToIDs()
		closeFile('\lua_escape:e{#1}_prestripped.tex')
	}
}

\cs_new_protected:Npn\stripsemantex_create_stripped_file:n#1
{
	\file_if_exist:nF { #1_prestripped.semtex }
	{
		\msg_fatal:nnn { stripsemantex } { no_semtex_file } { #1_prestripped }
	}

	\lua_now:e {
		copy_file('\lua_escape:e{#1}_prestripped.tex', '\lua_escape:e{#1}_stripped.tex')
		openFile('\lua_escape:e{#1}_stripped.tex')
	}
	
	\cs_set:Npn\SemantexID##1\BeginSource##2\EndSource\BeginOutput##3\EndOutput
	{
		\lua_now:e
		{
			semantexIDluacommand( '\lua_escape:e{\tl_trim_spaces:n{##1}}' , '\lua_escape:e{\exp_not:n{##2}}' , '\lua_escape:e{\exp_not:n{##3}}' )
		}
	}
	
	\cs_set:Npn\DeclareObject##1{}
	
	\cs_set:Npn\DeclareClass##1{}
	
	\cs_set:Npn\BeginSource##1\EndSource\BeginOutput##2\EndOutput{}
	
	\file_input:n { #1_prestripped.semtex }
	
	\lua_now:e
	{
		stripRemainingSemantexIDs()
		addSemtexPackageToFile()
		closeFile('\lua_escape:e{#1}_stripped.tex')
	}
}

\cs_set_eq:NN\StripSemantexCopyFile\stripsemantex_create_prestripped_file:n

\cs_new_protected:Npn\StripSemantexStripFile#1
{
	\begingroup
	\ExplSyntaxOn
	\stripsemantex_create_stripped_file:n { #1 }
%	\ExplSyntaxOff
	\endgroup
}

\cs_new_protected:Npn\StripSemantex#1
{
	% This will only work after a bug has been corrected in expl3
	\file_compare_timestamp:nNnTF { #1.semtex } < { #1_prestripped.semtex }
	{
		\StripSemantexStripFile { #1 }
	}
	{
		\StripSemantexCopyFile { #1 }
	}
}