Skip to content

Commit f8b665e

Browse files
committed
html service everything
1 parent 9180be9 commit f8b665e

20 files changed

Lines changed: 834 additions & 500 deletions

cli/src/translate.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <odr/file.hpp>
22
#include <odr/html.hpp>
3+
#include <odr/html_service.hpp>
34

45
#include <iostream>
56
#include <string>
@@ -33,7 +34,8 @@ int main(int argc, char **argv) {
3334
HtmlConfig config;
3435
config.editable = true;
3536

36-
html::translate(decoded_file, output, config);
37+
HtmlService service = html::translate(decoded_file, output, config);
38+
Html html = service.bring_offline(output);
3739

3840
return 0;
3941
}

src/odr/html.cpp

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <odr/exceptions.hpp>
77
#include <odr/filesystem.hpp>
88
#include <odr/global_params.hpp>
9+
#include <odr/html_service.hpp>
910

1011
#include <odr/internal/html/document.hpp>
1112
#include <odr/internal/html/filesystem.hpp>
@@ -27,36 +28,19 @@ namespace odr {
2728

2829
HtmlConfig::HtmlConfig() : resource_path{GlobalParams::odr_core_data_path()} {}
2930

30-
Html::Html(FileType file_type, HtmlConfig config, std::vector<HtmlPage> pages)
31-
: m_file_type{file_type}, m_config{std::move(config)},
32-
m_pages{std::move(pages)} {}
31+
Html::Html(HtmlConfig config, std::vector<HtmlPage> pages)
32+
: m_config{std::move(config)}, m_pages{std::move(pages)} {}
3333

34-
Html::Html(FileType file_type, HtmlConfig config, std::vector<HtmlPage> pages,
35-
Document document)
36-
: m_file_type{file_type}, m_config{std::move(config)},
37-
m_pages{std::move(pages)}, m_document{std::move(document)} {}
38-
39-
FileType Html::file_type() const { return m_file_type; }
34+
const HtmlConfig &Html::config() { return m_config; }
4035

4136
const std::vector<HtmlPage> &Html::pages() const { return m_pages; }
4237

43-
void Html::edit(const char *diff) {
44-
if (m_document) {
45-
html::edit(*m_document, diff);
46-
}
47-
}
48-
49-
void Html::save(const std::string &path) const {
50-
if (m_document) {
51-
m_document->save(path);
52-
}
53-
}
54-
5538
HtmlPage::HtmlPage(std::string name, std::string path)
5639
: name{std::move(name)}, path{std::move(path)} {}
5740

58-
Html html::translate(const DecodedFile &decoded_file,
59-
const std::string &output_path, const HtmlConfig &config) {
41+
HtmlService html::translate(const DecodedFile &decoded_file,
42+
const std::string &output_path,
43+
const HtmlConfig &config) {
6044
if (decoded_file.is_text_file()) {
6145
return translate(decoded_file.text_file(), output_path, config);
6246
} else if (decoded_file.is_image_file()) {
@@ -72,67 +56,74 @@ Html html::translate(const DecodedFile &decoded_file,
7256
throw UnsupportedFileType(decoded_file.file_type());
7357
}
7458

75-
Html html::translate(const TextFile &text_file, const std::string &output_path,
76-
const HtmlConfig &config) {
59+
HtmlService html::translate(const TextFile &text_file,
60+
const std::string &output_path,
61+
const HtmlConfig &config) {
7762
std::filesystem::create_directories(output_path);
78-
return internal::html::translate_text_file(text_file, output_path, config);
63+
return internal::html::create_text_service(text_file, output_path, config);
7964
}
8065

81-
Html html::translate(const ImageFile &image_file,
82-
const std::string &output_path, const HtmlConfig &config) {
66+
HtmlService html::translate(const ImageFile &image_file,
67+
const std::string &output_path,
68+
const HtmlConfig &config) {
8369
std::filesystem::create_directories(output_path);
84-
return internal::html::translate_image_file(image_file, output_path, config);
70+
return internal::html::create_image_service(image_file, output_path, config);
8571
}
8672

87-
Html html::translate(const ArchiveFile &archive_file,
88-
const std::string &output_path, const HtmlConfig &config) {
73+
HtmlService html::translate(const ArchiveFile &archive_file,
74+
const std::string &output_path,
75+
const HtmlConfig &config) {
8976
return translate(archive_file.archive(), output_path, config);
9077
}
9178

92-
Html html::translate(const DocumentFile &document_file,
93-
const std::string &output_path, const HtmlConfig &config) {
79+
HtmlService html::translate(const DocumentFile &document_file,
80+
const std::string &output_path,
81+
const HtmlConfig &config) {
9482
auto document_file_impl = document_file.impl();
9583

9684
#ifdef ODR_WITH_WVWARE
9785
if (auto wv_document_file =
9886
std::dynamic_pointer_cast<internal::WvWareLegacyMicrosoftFile>(
9987
document_file_impl)) {
10088
std::filesystem::create_directories(output_path);
101-
return internal::html::translate_wvware_oldms_file(*wv_document_file,
89+
return internal::html::create_wvware_oldms_service(*wv_document_file,
10290
output_path, config);
10391
}
10492
#endif
10593

10694
return translate(document_file.document(), output_path, config);
10795
}
10896

109-
Html html::translate(const PdfFile &pdf_file, const std::string &output_path,
110-
const HtmlConfig &config) {
97+
HtmlService html::translate(const PdfFile &pdf_file,
98+
const std::string &output_path,
99+
const HtmlConfig &config) {
111100
auto pdf_file_impl = pdf_file.impl();
112101

113102
#ifdef ODR_WITH_PDF2HTMLEX
114103
if (auto poppler_pdf_file =
115104
std::dynamic_pointer_cast<internal::PopplerPdfFile>(pdf_file_impl)) {
116105
std::filesystem::create_directories(output_path);
117-
return internal::html::translate_poppler_pdf_file(*poppler_pdf_file,
106+
return internal::html::create_poppler_pdf_service(*poppler_pdf_file,
118107
output_path, config);
119108
}
120109
#endif
121110

122-
return internal::html::translate_pdf_file(pdf_file, output_path, config);
111+
return internal::html::create_pdf_service(pdf_file, output_path, config);
123112
}
124113

125-
Html html::translate(const Archive &archive, const std::string &output_path,
126-
const HtmlConfig &config) {
114+
HtmlService html::translate(const Archive &archive,
115+
const std::string &output_path,
116+
const HtmlConfig &config) {
127117
std::filesystem::create_directories(output_path);
128-
return internal::html::translate_filesystem(
129-
FileType::unknown, archive.filesystem(), output_path, config);
118+
return internal::html::create_filesystem_service(archive.filesystem(),
119+
output_path, config);
130120
}
131121

132-
Html html::translate(const Document &document, const std::string &output_path,
133-
const HtmlConfig &config) {
122+
HtmlService html::translate(const Document &document,
123+
const std::string &output_path,
124+
const HtmlConfig &config) {
134125
std::filesystem::create_directories(output_path);
135-
return internal::html::translate_document(document, output_path, config);
126+
return internal::html::create_document_service(document, output_path, config);
136127
}
137128

138129
void html::edit(const Document &document, const char *diff) {

src/odr/html.hpp

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace odr {
1313
class Archive;
1414
struct HtmlPage;
15+
class HtmlService;
1516

1617
/// @brief HTML table gridlines.
1718
enum class HtmlTableGridlines {
@@ -64,25 +65,17 @@ struct HtmlConfig {
6465

6566
/// @brief HTML output.
6667
///
67-
/// Represents the output of a translated file to HTML. Also allows for editing
68-
/// and saving the output.
68+
/// Represents the output of a translated file to HTML.
6969
class Html final {
7070
public:
71-
Html(FileType file_type, HtmlConfig config, std::vector<HtmlPage> pages);
72-
Html(FileType file_type, HtmlConfig config, std::vector<HtmlPage> pages,
73-
Document document);
71+
Html(HtmlConfig config, std::vector<HtmlPage> pages);
7472

75-
[[nodiscard]] FileType file_type() const;
73+
[[nodiscard]] const HtmlConfig &config();
7674
[[nodiscard]] const std::vector<HtmlPage> &pages() const;
7775

78-
void edit(const char *diff);
79-
void save(const std::string &path) const;
80-
8176
private:
82-
FileType m_file_type;
8377
HtmlConfig m_config;
8478
std::vector<HtmlPage> m_pages;
85-
std::optional<Document> m_document;
8679
};
8780

8881
/// @brief HTML page.
@@ -103,66 +96,66 @@ namespace html {
10396
/// @param output_path Path to save the HTML output.
10497
/// @param config Configuration for the HTML output.
10598
/// @return HTML output.
106-
Html translate(const DecodedFile &file, const std::string &output_path,
107-
const HtmlConfig &config);
99+
HtmlService translate(const DecodedFile &file, const std::string &output_path,
100+
const HtmlConfig &config);
108101

109102
/// @brief Translates a text file to HTML.
110103
///
111104
/// @param text_file Text file to translate.
112105
/// @param output_path Path to save the HTML output.
113106
/// @param config Configuration for the HTML output.
114107
/// @return HTML output.
115-
Html translate(const TextFile &text_file, const std::string &output_path,
116-
const HtmlConfig &config);
108+
HtmlService translate(const TextFile &text_file, const std::string &output_path,
109+
const HtmlConfig &config);
117110
/// @brief Translates an image file to HTML.
118111
///
119112
/// @param image_file Image file to translate.
120113
/// @param output_path Path to save the HTML output.
121114
/// @param config Configuration for the HTML output.
122115
/// @return HTML output.
123-
Html translate(const ImageFile &image_file, const std::string &output_path,
124-
const HtmlConfig &config);
116+
HtmlService translate(const ImageFile &image_file,
117+
const std::string &output_path, const HtmlConfig &config);
125118
/// @brief Translates an archive to HTML.
126119
///
127120
/// @param archive Archive file to translate.
128121
/// @param output_path Path to save the HTML output.
129122
/// @param config Configuration for the HTML output.
130123
/// @return HTML output.
131-
Html translate(const ArchiveFile &archive_file, const std::string &output_path,
132-
const HtmlConfig &config);
124+
HtmlService translate(const ArchiveFile &archive_file,
125+
const std::string &output_path, const HtmlConfig &config);
133126
/// @brief Translates a document to HTML.
134127
///
135128
/// @param document_file Document file to translate.
136129
/// @param output_path Path to save the HTML output.
137130
/// @param config Configuration for the HTML output.
138131
/// @return HTML output.
139-
Html translate(const DocumentFile &document_file,
140-
const std::string &output_path, const HtmlConfig &config);
132+
HtmlService translate(const DocumentFile &document_file,
133+
const std::string &output_path, const HtmlConfig &config);
141134
/// @brief Translates a PDF file to HTML.
142135
///
143136
/// @param pdf_file PDF file to translate.
144137
/// @param output_path Path to save the HTML output.
145138
/// @param config Configuration for the HTML output.
146139
/// @return HTML output.
147-
Html translate(const PdfFile &pdf_file, const std::string &output_path,
148-
const HtmlConfig &config);
140+
HtmlService translate(const PdfFile &pdf_file, const std::string &output_path,
141+
const HtmlConfig &config);
149142

150143
/// @brief Translates an archive to HTML.
151144
///
152145
/// @param archive Archive to translate.
153146
/// @param output_path Path to save the HTML output.
154147
/// @param config Configuration for the HTML output.
155148
/// @return HTML output.
156-
Html translate(const Archive &archive, const std::string &output_path,
157-
const HtmlConfig &config);
149+
HtmlService translate(const Archive &archive, const std::string &output_path,
150+
const HtmlConfig &config);
158151
/// @brief Translates a document to HTML.
159152
///
160153
/// @param document Document to translate.
161154
/// @param output_path Path to save the HTML output.
162155
/// @param config Configuration for the HTML output.
163156
/// @return HTML output.
164-
Html translate(const Document &document, const std::string &output_path,
165-
const HtmlConfig &config);
157+
HtmlService translate(const Document &document, const std::string &output_path,
158+
const HtmlConfig &config);
166159

167160
/// @brief Edits a document with a diff.
168161
///

src/odr/html_service.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
#include <odr/html_service.hpp>
22

33
#include <odr/internal/abstract/html_service.hpp>
4+
#include <odr/internal/common/path.hpp>
45
#include <odr/internal/html/html_writer.hpp>
56

7+
#include <odr/exceptions.hpp>
8+
9+
#include <fstream>
610
#include <iostream>
711

812
namespace odr {
@@ -42,6 +46,57 @@ HtmlResources HtmlService::write_html(const std::string &path,
4246
return m_impl->write_html(path, writer);
4347
}
4448

49+
Html HtmlService::bring_offline(const std::string &output_path) const {
50+
return bring_offline(output_path, list_views());
51+
}
52+
53+
Html HtmlService::bring_offline(const std::string &output_path,
54+
const std::vector<HtmlView> &views) const {
55+
std::vector<HtmlPage> pages;
56+
57+
HtmlResources resources;
58+
59+
for (const auto &view : views) {
60+
std::filesystem::create_directories(
61+
std::filesystem::path(view.path()).parent_path());
62+
std::ofstream ostream(view.path(), std::ios::out);
63+
if (!ostream.is_open()) {
64+
throw FileWriteError();
65+
}
66+
67+
HtmlResources view_resources = view.write_html(ostream);
68+
resources.insert(resources.end(), view_resources.begin(),
69+
view_resources.end());
70+
71+
pages.emplace_back(view.name(), view.path());
72+
}
73+
74+
{
75+
auto it = std::unique(resources.begin(), resources.end(),
76+
[](const auto &lhs, const auto &rhs) {
77+
return lhs.first.path() == rhs.first.path();
78+
});
79+
resources.erase(it, resources.end());
80+
}
81+
82+
for (const auto &[resource, location] : resources) {
83+
if (!location.has_value()) {
84+
continue;
85+
}
86+
auto path = odr::internal::common::Path(output_path)
87+
.join(odr::internal::common::Path(*location));
88+
89+
std::filesystem::create_directories(path.parent().path());
90+
std::ofstream ostream(path.path(), std::ios::out);
91+
if (!ostream.is_open()) {
92+
throw FileWriteError();
93+
}
94+
resource.write_resource(ostream);
95+
}
96+
97+
return {config(), std::move(pages)};
98+
}
99+
45100
const std::shared_ptr<internal::abstract::HtmlService> &
46101
HtmlService::impl() const {
47102
return m_impl;
@@ -63,6 +118,38 @@ HtmlResources HtmlView::write_html(std::ostream &out) const {
63118
return m_impl->write_html(writer);
64119
}
65120

121+
Html HtmlView::bring_offline(const std::string &output_path) const {
122+
HtmlResources resources;
123+
124+
{
125+
std::filesystem::create_directories(
126+
std::filesystem::path(path()).parent_path());
127+
std::ofstream ostream(path(), std::ios::out);
128+
if (!ostream.is_open()) {
129+
throw FileWriteError();
130+
}
131+
132+
resources = write_html(ostream);
133+
}
134+
135+
for (const auto &[resource, location] : resources) {
136+
if (!location.has_value()) {
137+
continue;
138+
}
139+
auto path = odr::internal::common::Path(output_path)
140+
.join(odr::internal::common::Path(*location));
141+
142+
std::filesystem::create_directories(path.parent().path());
143+
std::ofstream ostream(path.path(), std::ios::out);
144+
if (!ostream.is_open()) {
145+
throw FileWriteError();
146+
}
147+
resource.write_resource(ostream);
148+
}
149+
150+
return {config(), {{name(), path()}}};
151+
}
152+
66153
const std::shared_ptr<internal::abstract::HtmlView> &HtmlView::impl() const {
67154
return m_impl;
68155
}

0 commit comments

Comments
 (0)