NoTeX 1.0.0
A modern noteworthy LaTeX template
Loading...
Searching...
No Matches
templates.cpp
1
9
10#include "notex/templates.hpp"
11
12#include <cctype>
13#include <cstddef>
14
15namespace notex::templates {
16
17namespace {
18
19std::string replace_all(std::string text, std::string_view token,
20 std::string_view replacement) {
21 std::size_t pos = 0;
22 while ((pos = text.find(token, pos)) != std::string::npos) {
23 text.replace(pos, token.size(), replacement);
24 pos += replacement.size();
25 }
26 return text;
27}
28
29std::string slugify(std::string_view title) {
30 std::string result;
31 result.reserve(title.size());
32 bool last_was_separator = true; // Avoids a leading underscore.
33 for (const char raw : title) {
34 const auto c = static_cast<unsigned char>(raw);
35 if (std::isalnum(c)) {
36 result.push_back(static_cast<char>(std::tolower(c)));
37 last_was_separator = false;
38 } else if (!last_was_separator) {
39 result.push_back('_');
40 last_was_separator = true;
41 }
42 }
43 while (!result.empty() && result.back() == '_') { result.pop_back(); }
44 return result;
45}
46
47constexpr std::string_view kMonoMainTemplate =
48 R"NOTEX(\documentclass[light]{@CLASS@}
49
50% PREAMBLE ────────────────────────────────────────────────────────────────
51
52% Additional packages here
53
54% Title, Author and Date
55\title{My Notes}
56\author{}
57\date{\today}
58
59% DOCUMENT ────────────────────────────────────────────────────────────────
60\begin{document}
61
62% Title page
63\maketitle
64\tableofcontents
65
66% Sections
67
68\section{Introduction}
69
70
71
72\end{document}
73)NOTEX";
74
75constexpr std::string_view kMultiMainTemplate =
76 R"NOTEX(\documentclass[light]{@CLASS@}
77
78% PREAMBLE ────────────────────────────────────────────────────────────────
79
80% Additional packages here
81\usepackage{subfiles} % Modular document structure (best loaded last)
82
83% Title, Author and Date
84\title{My Notes}
85\author{}
86\date{\today}
87
88% DOCUMENT ────────────────────────────────────────────────────────────────
89\begin{document}
90
91% Title page
92\maketitle
93\tableofcontents
94
95% Sections
96\pagebreak
97@SECTIONS@
98\end{document}
99)NOTEX";
100
101constexpr std::string_view kSectionTemplate =
102 R"NOTEX(\documentclass[../main.tex]{subfiles}
103
104\begin{document}
105
106% Section @NUMBER@
107\section{@TITLE@}
108
109
110
111\end{document}
112)NOTEX";
113
114constexpr std::string_view kBibliographyStarter =
115 R"NOTEX(@misc{key,
116 author = "",
117 title = "",
118 url = "",
119}
120)NOTEX";
121
122} // namespace
123
124std::string_view to_string(ProjectType type) {
125 switch (type) {
127 return "mono";
129 return "multi";
130 }
131 return "multi";
132}
133
134std::optional<ProjectType> project_type_from_string(std::string_view name) {
135 if (name == "mono") return ProjectType::MONO;
136 if (name == "multi") return ProjectType::MULTI;
137 return std::nullopt;
138}
139
140std::string mono_main(std::string_view class_path) {
141 return replace_all(std::string(kMonoMainTemplate), "@CLASS@", class_path);
142}
143
144std::string multi_main(std::string_view class_path,
145 const std::vector<std::string>& section_stems) {
146 std::string sections_block;
147 for (std::size_t i = 0; i < section_stems.size(); ++i) {
148 if (i != 0) sections_block += "\\pagebreak\n";
149 sections_block += "\\subfile{sections/" + section_stems[i] + "}\n";
150 }
151
152 std::string result =
153 replace_all(std::string(kMultiMainTemplate), "@CLASS@", class_path);
154 return replace_all(std::move(result), "@SECTIONS@", sections_block);
155}
156
157std::string section_stem(int number, std::string_view title) {
158 return std::to_string(number) + "_" + slugify(title);
159}
160
161std::string section(int number, std::string_view title) {
162 std::string result =
163 replace_all(std::string(kSectionTemplate), "@TITLE@", title);
164 return replace_all(std::move(result), "@NUMBER@", std::to_string(number));
165}
166
167std::string bibliography_starter() {
168 return std::string(kBibliographyStarter);
169}
170
171} // namespace notex::templates
std::string section(int number, std::string_view title)
Generates a numbered section subfile.
std::string mono_main(std::string_view class_path)
Generates the entry file for a single-file project.
std::string multi_main(std::string_view class_path, const std::vector< std::string > &section_stems={"1_introduction"})
Generates the entry file for a multi-file project, referencing section_stems in order.
std::string bibliography_starter()
std::string section_stem(int number, std::string_view title)
Computes the filename stem (no directory, no extension) for a numbered section.
@ MULTI
main.tex plus a sections/ directory of subfiles.
Definition templates.hpp:24
@ MONO
A single main.tex file.
Definition templates.hpp:23
std::optional< ProjectType > project_type_from_string(std::string_view name)
Parses a project type name.
std::string_view to_string(ProjectType type)
templates: encoded skeletons used to scaffold new NoTeX projects.