\RequirePackage{xparse} \ProvidesExplPackage{stripsemantex}{2020/09/10}{0.42}{} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % 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') } \DeclareDocumentCommand\SemantexID{ m t{\BeginSource} } { \IfBooleanT{##2} { \strip_semantex_id_command:w ##1 \BeginSource } } \cs_set:Npn\strip_semantex_id_command:w##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 } } }