Skip to content

Instantly share code, notes, and snippets.

@Chubek
Last active September 24, 2024 08:36
Show Gist options
  • Save Chubek/ea179729b796debb5afe129639ad1889 to your computer and use it in GitHub Desktop.
Save Chubek/ea179729b796debb5afe129639ad1889 to your computer and use it in GitHub Desktop.
SimpleCNF; a Configuration Management package for LaTeX

The file simplecnf.sty contains a very simple 'Configuration Management' facility to use with LaTeX.

It accepts INI-like files, either as files:

foo = bar
sipyek = nod
\cnfloadfile{myconf.cnf}

Or as strings:

\cnfloadstring{foo = bar \par sipyek = nod}

And you can later use \cnfget to access the item:

\cnfget{foo} % gets 'bar'

Just don't use \par in files. Only use them in strings. Use the system newline (0xa, 0xc0xa) in files.

I write packages for personal use in my literate programs. Do you need to typset Text-rewriting rules? Check this out!

And my other work.

\NeedsTeXFormat{LaTeX2e}
\RequirePackage{expl3}
\ProvidesExplPackage{simplecnf}
{ 2024-09-22 }
{ 1.0.0 }
{ Provides parsing of INI-like setting files }
\prop_new:N \g_cnf_prop
\cs_new_protected:Npn \parse_config_line:n #1 {
\seq_set_split:Nnn \l_tmpa_seq { = } { #1 }
\seq_pop_left:NN \l_tmpa_seq \l_tmpa_tl
\seq_pop_left:NN \l_tmpa_seq \l_tmpb_tl
\prop_put:Nxx \g_cnf_prop
{ \exp_args:NV \tl_trim_spaces:n \l_tmpa_tl }
{ \exp_args:NV \tl_trim_spaces:n \l_tmpb_tl }
}
\cs_new_protected:Npn \parse_config:n #1 {
% Split the config by lines
\seq_set_split:Nnn \l_tmpa_seq { \par } { #1 }
% Loop through each line
\seq_map_inline:Nn \l_tmpa_seq {
\parse_config_line:n { ##1 }
}
}
\cs_new_protected:Npn \config_get:n #1 {
\prop_get:NnNTF \g_cnf_prop { #1 } \l_tmpa_tl
{ \tl_use:N \l_tmpa_tl }
{ \PackagError{ Key~'#1'~not~found } }
}
\cs_new_protected:Npn \parse_config_file:n #1 {
\ior_new:N \l_cnf_ior
\ior_open:Nn \l_cnf_ior #1
\ior_str_get:NN \l_cnf_ior \l_tmpa_tl
\ior_map_inline:Nn \l_cnf_ior
{ \tl_put_right:Nn \l_tmpa_tl { \par ##1 } }
\ior_close:N \l_cnf_ior
\exp_args:No \parse_config:n \l_tmpa_tl
}
\newcommand\cnfloadfile[1]
{ \exp_args:Nx \parse_config_file:n {#1} }
\newcommand\cnfloadstring[1]
{ \exp_args:Nx \parse_config:n {#1} }
\newcommand\cnfget[1]
{ \exp_args:Nx \config_get:n {#1} }
\newcommand\cnflistkeys
{ \prop_map_inline:Nn \g_cnf_prop { CnfKey:~##1\par } }
\newcommand\cnflistvalues
{ \prop_map_inline:Nn \g_cnf_prop { CnfValue:~##2\par } }
\newcommand\cnflistkeyvalues
{ \prop_map_inline:Nn \g_cnf_prop { CnfPair:~##1~=~##2\par } }
\endinput
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment