summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/jobname-suffix/jobname-suffix.sty
blob: d57014ff96e95103bcbf768fc1855ef6e920bb1d (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesExplPackage{jobname-suffix}{2022/09/17}{1.0}{Compile differently based on the filename}

\RequirePackage{expl3}
\ExplSyntaxOn

%
%  Variants
%
\cs_generate_variant:Nn \str_set:Nn {Ne}
\cs_generate_variant:Nn \str_if_eq:nnTF {nfTF}

%
% Messages
%
\msg_new:nnn {jobname-suffix} {initial-data} {
	\\
	(jobname-suffix)\ \ \ Found~jobname~~`#1'~~with~suffix~~`#2'
	\\
}
\msg_new:nnn {jobname-suffix} {override} {
	\\
	(jobname-suffix)\ \ \ Overriding~suffix~with~`#1'
	\\
}
\msg_new:nnn {jobname-suffix} {test} {
	\\
	(jobname-suffix)\ \ \ Testing~`#1'~==~`#2'
	\\
}
\msg_new:nnn {jobname-suffix} {condition} {
	\\
	(jobname-suffix)\ \ \ Condition~is~`#1'
	\\
}





\str_new:N \g__suffix_str
\str_new:N \g__jobname_str
\str_new:N \g__split_char_str
\str_set:Nn \g__split_char_str {-}
\seq_new:N \l__suffix_list_seq
\str_new:N \l__suffix_list_str





\int_new:N \l__slice_pos_int
% Slice #1 at the char indicated by #2 and store the result in #3
\cs_new_protected:Nn \__slice_at_char:NNN {
	\int_set:Nn \l__slice_pos_int {1}
	\str_map_inline:Nn #1 {
		\int_incr:N \l__slice_pos_int
		\str_if_eq:VnTF {#2} {##1} {
			\str_map_break:
		}{}
	}
	\str_set:Nx #3 {\str_range:Nnn #1 {\l__slice_pos_int}{\c_max_int}}
}


% If the string starts with a quote, trim it off.
\cs_new:Nn \__trim_quotes:N {
	\str_if_eq:nfTF {"} { \str_range:Nnn #1 {1}{1}} {
		\str_range:Nnn #1 {2}{-1+\str_count:N #1}
	}{
		#1
	}
}

% The jobname will be surrounded by quotes if there are any spaces in it,
% so we first trim it if needed.
\str_set:Nx \g__jobname_str { \__trim_quotes:N \c_sys_jobname_str }
% Find the suffix string. The result will be stored in the last argument
\__slice_at_char:NNN \g__jobname_str \g__split_char_str \g__suffix_str

\msg_term:nnxx {jobname-suffix} {initial-data} {\g__jobname_str} {\g__suffix_str}

%
% User-facing commands and environments
%

% Optional arg is a comma-separate list of suffixes. If any of the listed
% suffixes match the jobname's suffix, the true contiditon is executed. Otherwise
% the false condition is executed.
\NewDocumentCommand{\IfSuffixTF}{ o +m +m }{
	%\msg_term:nnxx {jobname-suffix} {test} {#1} {\g__suffix_str}

	\IfNoValueTF {#1} {
		%\msg_term:nnx {jobname-suffix} {condition} {FALSE}
		#3
	}{
		% We want to do string-based comparisons, so we must first
		% cast our argument list as a string (otherwise, \seq_* operations
		% won't work properly).
		\str_set:Nn \l__suffix_list_str {#1}
		\seq_set_split:NnV \l__suffix_list_seq {,} {\l__suffix_list_str}

		\seq_if_in:NVTF \l__suffix_list_seq {\g__suffix_str} {
			%\msg_term:nnx {jobname-suffix} {condition} {TRUE}
			#2
		} {
			%\msg_term:nnx {jobname-suffix} {condition} {FALSE}
			#3
		}
	}
}

\NewDocumentCommand{\IfSuffixT}{ o +m }{
	\IfSuffixTF[#1]{#2}{}
}

\NewDocumentCommand{\IfSuffixF}{ o +m }{
	\IfSuffixTF[#1]{}{#2}
}

\NewDocumentEnvironment{IfSuffix}{ o +b }{
	\IfSuffixT[#1]{#2}
}{}

\NewDocumentCommand{\OverrideSuffix}{ m }{
	\str_set:Nn \g__suffix_str {#1}
	\msg_term:nnx {jobname-suffix} {override} {\g__suffix_str}
}
\NewExpandableDocumentCommand{\JobnameSuffix}{}{
	\str_use:N \g__suffix_str
}


\ExplSyntaxOff