Skip to content

Commit 25b3c19

Browse files
committed
use template files from pre-defined location for HTML generation
This simplifies finding those files. Furthermore, those files are now created by the program, if they are needed but don't exist. Default template codes are used in that case.
1 parent 4cef688 commit 25b3c19

File tree

13 files changed

+350
-7
lines changed

13 files changed

+350
-7
lines changed

code/CMakeLists.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ set(pmdb_sources
3131
functions.cpp
3232
html_generation.cpp
3333
paths.cpp
34+
templates/defaults.hpp
35+
templates/functions.cpp
3436
../libstriezel/common/DirectoryFileList.cpp
3537
../libstriezel/common/StringUtils.cpp
3638
../libstriezel/filesystem/directory.cpp
@@ -81,3 +83,44 @@ if (ZLIB_FOUND)
8183
else ()
8284
message ( FATAL_ERROR "zlib was not found!" )
8385
endif (ZLIB_FOUND)
86+
87+
## get the default templates
88+
execute_process(
89+
COMMAND cat folder.tpl
90+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/templates
91+
OUTPUT_VARIABLE FOLDER_TPL
92+
OUTPUT_STRIP_TRAILING_WHITESPACE
93+
)
94+
95+
execute_process(
96+
COMMAND cat folder_entry.tpl
97+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/templates
98+
OUTPUT_VARIABLE FOLDER_ENTRY_TPL
99+
OUTPUT_STRIP_TRAILING_WHITESPACE
100+
)
101+
102+
execute_process(
103+
COMMAND cat folder_list.tpl
104+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/templates
105+
OUTPUT_VARIABLE FOLDER_LIST_TPL
106+
OUTPUT_STRIP_TRAILING_WHITESPACE
107+
)
108+
109+
execute_process(
110+
COMMAND cat index_entry.tpl
111+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/templates
112+
OUTPUT_VARIABLE INDEX_ENTRY_TPL
113+
OUTPUT_STRIP_TRAILING_WHITESPACE
114+
)
115+
116+
execute_process(
117+
COMMAND cat message.tpl
118+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/templates
119+
OUTPUT_VARIABLE MESSAGE_TPL
120+
OUTPUT_STRIP_TRAILING_WHITESPACE
121+
)
122+
123+
# replace placeholders in defaults.prototype.hpp and create defaults.hpp
124+
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/templates/defaults.prototype.hpp
125+
${CMAKE_CURRENT_SOURCE_DIR}/templates/defaults.hpp
126+
@ONLY)

code/html_generation.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "bbcode/HorizontalRuleBBCode.hpp"
3030
#include "bbcode/ListBBCode.hpp"
3131
#include "bbcode/TableBBCode.hpp"
32+
#include "templates/functions.hpp"
3233
#include "../libstriezel/filesystem/directory.hpp"
3334
#include "../libstriezel/filesystem/file.hpp"
3435

@@ -80,12 +81,20 @@ int generateHtmlFiles(const MessageDatabase& mdb, const FolderMap& fm, const HTM
8081
}
8182
#endif
8283

84+
// Ensure that template files exist.
85+
const int tpl_exit_code = pmdb::tpl::ensureFilesExist();
86+
if (tpl_exit_code != 0)
87+
{
88+
return tpl_exit_code;
89+
}
90+
8391
// load template for HTML files
8492
MsgTemplate theTemplate;
85-
if (!theTemplate.loadFromFile(conf.getTPL()))
93+
const auto template_directory = libstriezel::filesystem::slashify(pmdb::paths::templates());
94+
if (!theTemplate.loadFromFile(template_directory + "message.tpl"))
8695
{
87-
std::cerr << "Error: Could not load template file \"" << conf.getTPL()
88-
<< "\" for messages!\n";
96+
std::cerr << "Error: Could not load template file \"" << template_directory
97+
<< "message.tpl\" for messages!\n";
8998
return rcFileError;
9099
}
91100

@@ -170,22 +179,22 @@ int generateHtmlFiles(const MessageDatabase& mdb, const FolderMap& fm, const HTM
170179
} // while
171180
// create index file
172181
MsgTemplate tplIndex, tplEntry, tplFolderList, tplFolderEntry;
173-
if (!tplIndex.loadFromFile("folder.tpl"))
182+
if (!tplIndex.loadFromFile(template_directory + "folder.tpl"))
174183
{
175184
std::cerr << "Could not load folder.tpl!\n";
176185
return rcFileError;
177186
}
178-
if (!tplEntry.loadFromFile("index_entry.tpl"))
187+
if (!tplEntry.loadFromFile(template_directory + "index_entry.tpl"))
179188
{
180189
std::cerr << "Could not load index_entry.tpl!\n";
181190
return rcFileError;
182191
}
183-
if (!tplFolderList.loadFromFile("folder_list.tpl"))
192+
if (!tplFolderList.loadFromFile(template_directory + "folder_list.tpl"))
184193
{
185194
std::cerr << "Could not load folder_list.tpl!\n";
186195
return rcFileError;
187196
}
188-
if (!tplFolderEntry.loadFromFile("folder_entry.tpl"))
197+
if (!tplFolderEntry.loadFromFile(template_directory + "folder_entry.tpl"))
189198
{
190199
std::cerr << "Could not load folder_entry.tpl!\n";
191200
return rcFileError;

code/paths.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ std::string paths::html()
4040
return main() + libstriezel::filesystem::pathDelimiter + "html";
4141
}
4242

43+
std::string paths::templates()
44+
{
45+
return main() + libstriezel::filesystem::pathDelimiter + "templates";
46+
}
47+
4348
std::string paths::conf()
4449
{
4550
return main() + libstriezel::filesystem::pathDelimiter + "pmdb.conf";

code/paths.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ struct paths
4343
*/
4444
static std::string html();
4545

46+
/** \brief Gets the directory where HTML template files are saved.
47+
*
48+
* \return Returns the directory where HTML template files are saved.
49+
* \remarks This is usually ~/.pmdb/templates or the equivalent.
50+
*/
51+
static std::string templates();
4652

4753
/** \brief Gets the default path for the configuration file.
4854
*

code/pmdb.cbp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@
152152
<Unit filename="main.cpp" />
153153
<Unit filename="paths.cpp" />
154154
<Unit filename="paths.hpp" />
155+
<Unit filename="templates/defaults.hpp" />
156+
<Unit filename="templates/functions.cpp" />
157+
<Unit filename="templates/functions.hpp" />
155158
<Extensions>
156159
<lib_finder disable_auto="1" />
157160
</Extensions>

code/templates/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Ignore defaults.hpp, because it is generated from template during the CMake
2+
# build process.
3+
defaults.hpp
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
-------------------------------------------------------------------------------
3+
This file is part of the Private Message Database.
4+
Copyright (C) 2025 Dirk Stolle
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
-------------------------------------------------------------------------------
19+
*/
20+
21+
#ifndef PMDB_TEMPLATES_DEFAULTS_HPP
22+
#define PMDB_TEMPLATES_DEFAULTS_HPP
23+
24+
#include <string_view>
25+
26+
namespace pmdb::tpl::defaults
27+
{
28+
29+
using namespace std::string_view_literals;
30+
31+
constexpr std::string_view folder = R"(@FOLDER_TPL@)"sv;
32+
33+
constexpr std::string_view folder_entry = R"(@FOLDER_ENTRY_TPL@)"sv;
34+
35+
constexpr std::string_view folder_list = R"(@FOLDER_LIST_TPL@)"sv;
36+
37+
constexpr std::string_view index_entry = R"(@INDEX_ENTRY_TPL@)"sv;
38+
39+
constexpr std::string_view message = R"(@MESSAGE_TPL@)"sv;
40+
41+
} // namespace
42+
43+
#endif // PMDB_TEMPLATES_DEFAULTS_HPP

code/templates/functions.cpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
-------------------------------------------------------------------------------
3+
This file is part of the Private Message Database test suite.
4+
Copyright (C) 2025 Dirk Stolle
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
-------------------------------------------------------------------------------
19+
*/
20+
21+
#include "functions.hpp"
22+
#include <fstream>
23+
#include <iostream>
24+
#include "../../libstriezel/filesystem/directory.hpp"
25+
#include "../../libstriezel/filesystem/file.hpp"
26+
#include "../paths.hpp"
27+
#include "../ReturnCodes.hpp"
28+
#include "defaults.hpp"
29+
30+
namespace pmdb::tpl
31+
{
32+
33+
int ensureTemplateFile(const std::string& file_path, const std::string_view default_content)
34+
{
35+
if (!libstriezel::filesystem::file::exists(file_path))
36+
{
37+
std::ofstream stream(file_path, std::ios::out | std::ios::binary);
38+
if (!stream)
39+
{
40+
std::cerr << "Error: Could not create template file " << file_path << "!\n";
41+
return rcFileError;
42+
}
43+
stream.write(default_content.data(), default_content.size());
44+
stream.close();
45+
if (!stream.good())
46+
{
47+
std::cerr << "Error: Could not write template file " << file_path << "!\n";
48+
return rcFileError;
49+
}
50+
}
51+
52+
// File already existed or was created successfully.
53+
return 0;
54+
}
55+
56+
int ensureFilesExist()
57+
{
58+
const auto directory = pmdb::paths::templates();
59+
if (!libstriezel::filesystem::directory::exists(directory))
60+
{
61+
if (!libstriezel::filesystem::directory::createRecursive(directory))
62+
{
63+
std::cerr << "Error: Could not create missing template directory "
64+
<< directory << ".\n";
65+
return rcFileError;
66+
}
67+
}
68+
69+
const auto tpl_directory = libstriezel::filesystem::slashify(directory);
70+
71+
int rc = ensureTemplateFile(tpl_directory + "message.tpl", pmdb::tpl::defaults::message);
72+
if (rc != 0)
73+
{
74+
return rc;
75+
}
76+
rc = ensureTemplateFile(tpl_directory + "folder.tpl", pmdb::tpl::defaults::folder);
77+
if (rc != 0)
78+
{
79+
return rc;
80+
}
81+
rc = ensureTemplateFile(tpl_directory + "index_entry.tpl", pmdb::tpl::defaults::index_entry);
82+
if (rc != 0)
83+
{
84+
return rc;
85+
}
86+
rc = ensureTemplateFile(tpl_directory + "folder_entry.tpl", pmdb::tpl::defaults::folder_entry);
87+
if (rc != 0)
88+
{
89+
return rc;
90+
}
91+
return ensureTemplateFile(tpl_directory + "folder_list.tpl", pmdb::tpl::defaults::folder_list);
92+
}
93+
94+
} // namespace

code/templates/functions.hpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
-------------------------------------------------------------------------------
3+
This file is part of the Private Message Database test suite.
4+
Copyright (C) 2025 Dirk Stolle
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
-------------------------------------------------------------------------------
19+
*/
20+
21+
#ifndef PMDB_TPL_FUNCTIONS_HPP
22+
#define PMDB_TPL_FUNCTIONS_HPP
23+
24+
namespace pmdb::tpl
25+
{
26+
27+
/** \brief Ensures that all required template files for HTML code generation
28+
* exist by creating missing files with default template content.
29+
*
30+
* \return Returns zero in case of success.
31+
* Returns a non-zero exit code, if an error occurred.
32+
*/
33+
int ensureFilesExist();
34+
35+
} // namespace
36+
37+
#endif // PMDB_TPL_FUNCTIONS_HPP

program-no-compression/CMakeLists.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ set(pmdb_no_comp_sources
3737
../code/functions.cpp
3838
../code/html_generation.cpp
3939
../code/paths.cpp
40+
../code/templates/defaults.hpp
41+
../code/templates/functions.cpp
4042
../libstriezel/common/DirectoryFileList.cpp
4143
../libstriezel/common/StringUtils.cpp
4244
../libstriezel/filesystem/directory.cpp
@@ -69,3 +71,44 @@ if (LIBXML2_FOUND)
6971
else ()
7072
message ( FATAL_ERROR "libxml2 was not found!" )
7173
endif (LIBXML2_FOUND)
74+
75+
# get the default templates
76+
execute_process(
77+
COMMAND cat folder.tpl
78+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../code/templates
79+
OUTPUT_VARIABLE FOLDER_TPL
80+
OUTPUT_STRIP_TRAILING_WHITESPACE
81+
)
82+
83+
execute_process(
84+
COMMAND cat folder_entry.tpl
85+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../code/templates
86+
OUTPUT_VARIABLE FOLDER_ENTRY_TPL
87+
OUTPUT_STRIP_TRAILING_WHITESPACE
88+
)
89+
90+
execute_process(
91+
COMMAND cat folder_list.tpl
92+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../code/templates
93+
OUTPUT_VARIABLE FOLDER_LIST_TPL
94+
OUTPUT_STRIP_TRAILING_WHITESPACE
95+
)
96+
97+
execute_process(
98+
COMMAND cat index_entry.tpl
99+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../code/templates
100+
OUTPUT_VARIABLE INDEX_ENTRY_TPL
101+
OUTPUT_STRIP_TRAILING_WHITESPACE
102+
)
103+
104+
execute_process(
105+
COMMAND cat message.tpl
106+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../code/templates
107+
OUTPUT_VARIABLE MESSAGE_TPL
108+
OUTPUT_STRIP_TRAILING_WHITESPACE
109+
)
110+
111+
# replace placeholders in defaults.prototype.hpp and create defaults.hpp
112+
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/../code/templates/defaults.prototype.hpp
113+
${CMAKE_CURRENT_SOURCE_DIR}/../code/templates/defaults.hpp
114+
@ONLY)

0 commit comments

Comments
 (0)