66#include < odr/exceptions.hpp>
77#include < odr/filesystem.hpp>
88#include < odr/global_params.hpp>
9- #include < odr/html_service.hpp>
109
10+ #include < odr/internal/abstract/html_service.hpp>
11+ #include < odr/internal/common/path.hpp>
1112#include < odr/internal/html/document.hpp>
1213#include < odr/internal/html/filesystem.hpp>
14+ #include < odr/internal/html/html_writer.hpp>
1315#include < odr/internal/html/image_file.hpp>
1416#include < odr/internal/html/pdf2htmlex_wrapper.hpp>
1517#include < odr/internal/html/pdf_file.hpp>
1618#include < odr/internal/html/text_file.hpp>
1719#include < odr/internal/html/wvware_wrapper.hpp>
1820#include < odr/internal/oldms_wvware/wvware_oldms_file.hpp>
1921#include < odr/internal/pdf_poppler/poppler_pdf_file.hpp>
22+ #include < odr/internal/util/file_util.hpp>
2023
24+ #include < algorithm>
2125#include < filesystem>
26+ #include < fstream>
27+ #include < iostream>
2228
2329#include < nlohmann/json.hpp>
2430
2531using namespace odr ::internal;
2632
2733namespace odr {
2834
35+ namespace {
36+
37+ void bring_offline (const HtmlResources &resources,
38+ const std::string &output_path) {
39+ for (const auto &[resource, location] : resources) {
40+ if (!location.has_value () || resource.is_shipped () ||
41+ resource.is_external () || !resource.is_accessible ()) {
42+ continue ;
43+ }
44+ auto path = odr::internal::common::Path (output_path)
45+ .join (odr::internal::common::Path (*location));
46+
47+ std::filesystem::create_directories (path.parent ());
48+ std::ofstream ostream = internal::util::file::create (path.string ());
49+ resource.write_resource (ostream);
50+ }
51+ }
52+
53+ } // namespace
54+
2955HtmlConfig::HtmlConfig () { init (); }
3056
3157HtmlConfig::HtmlConfig (std::string output_path_)
@@ -48,6 +74,144 @@ const std::vector<HtmlPage> &Html::pages() const { return m_pages; }
4874HtmlPage::HtmlPage (std::string name, std::string path)
4975 : name{std::move (name)}, path{std::move (path)} {}
5076
77+ HtmlService::HtmlService () = default ;
78+
79+ HtmlService::HtmlService (std::shared_ptr<internal::abstract::HtmlService> impl)
80+ : m_impl{std::move (impl)} {}
81+
82+ const HtmlConfig &HtmlService::config () const { return m_impl->config (); }
83+
84+ const HtmlViews &HtmlService::list_views () const {
85+ return m_impl->list_views ();
86+ }
87+
88+ void HtmlService::warmup () const { m_impl->warmup (); }
89+
90+ bool HtmlService::exists (const std::string &path) const {
91+ return m_impl->exists (path);
92+ }
93+
94+ std::string HtmlService::mimetype (const std::string &path) const {
95+ return m_impl->mimetype (path);
96+ }
97+
98+ void HtmlService::write (const std::string &path, std::ostream &out) const {
99+ m_impl->write (path, out);
100+ }
101+
102+ HtmlResources HtmlService::write_html (const std::string &path,
103+ std::ostream &out) const {
104+ internal::html::HtmlWriter writer (out, config ());
105+ return m_impl->write_html (path, writer);
106+ }
107+
108+ Html HtmlService::bring_offline (const std::string &output_path) const {
109+ return bring_offline (output_path, list_views ());
110+ }
111+
112+ Html HtmlService::bring_offline (const std::string &output_path,
113+ const std::vector<HtmlView> &views) const {
114+ std::vector<HtmlPage> pages;
115+
116+ HtmlResources resources;
117+
118+ for (const auto &view : views) {
119+ auto path = odr::internal::common::Path (output_path)
120+ .join (odr::internal::common::Path (view.path ()));
121+
122+ std::filesystem::create_directories (path.parent ());
123+ std::ofstream ostream = internal::util::file::create (path.string ());
124+ HtmlResources view_resources = view.write_html (ostream);
125+
126+ resources.insert (resources.end (), view_resources.begin (),
127+ view_resources.end ());
128+
129+ pages.emplace_back (view.name (), path.string ());
130+ }
131+
132+ {
133+ auto it = std::unique (resources.begin (), resources.end (),
134+ [](const auto &lhs, const auto &rhs) {
135+ return lhs.first .path () == rhs.first .path ();
136+ });
137+ resources.erase (it, resources.end ());
138+ }
139+
140+ odr::bring_offline (resources, output_path);
141+
142+ return {config (), std::move (pages)};
143+ }
144+
145+ const std::shared_ptr<internal::abstract::HtmlService> &
146+ HtmlService::impl () const {
147+ return m_impl;
148+ }
149+
150+ HtmlView::HtmlView () = default ;
151+
152+ HtmlView::HtmlView (std::shared_ptr<internal::abstract::HtmlView> impl)
153+ : m_impl{std::move (impl)} {}
154+
155+ const std::string &HtmlView::name () const { return m_impl->name (); }
156+
157+ const std::string &HtmlView::path () const { return m_impl->path (); }
158+
159+ const HtmlConfig &HtmlView::config () const { return m_impl->config (); }
160+
161+ HtmlResources HtmlView::write_html (std::ostream &out) const {
162+ internal::html::HtmlWriter writer (out, config ());
163+ return m_impl->write_html (writer);
164+ }
165+
166+ Html HtmlView::bring_offline (const std::string &output_path) const {
167+ HtmlResources resources;
168+
169+ auto path = odr::internal::common::Path (output_path)
170+ .join (odr::internal::common::Path (this ->path ()));
171+
172+ {
173+ std::filesystem::create_directories (path.parent ());
174+ std::ofstream ostream = internal::util::file::create (path.string ());
175+ resources = write_html (ostream);
176+ }
177+
178+ odr::bring_offline (resources, output_path);
179+
180+ return {config (), {{name (), path.string ()}}};
181+ }
182+
183+ const std::shared_ptr<internal::abstract::HtmlView> &HtmlView::impl () const {
184+ return m_impl;
185+ }
186+
187+ HtmlResource::HtmlResource () = default ;
188+
189+ HtmlResource::HtmlResource (
190+ std::shared_ptr<internal::abstract::HtmlResource> impl)
191+ : m_impl{std::move (impl)} {}
192+
193+ HtmlResourceType HtmlResource::type () const { return m_impl->type (); }
194+
195+ const std::string &HtmlResource::mime_type () const {
196+ return m_impl->mime_type ();
197+ }
198+
199+ const std::string &HtmlResource::name () const { return m_impl->name (); }
200+
201+ const std::string &HtmlResource::path () const { return m_impl->path (); }
202+
203+ const std::optional<File> &HtmlResource::file () const { return m_impl->file (); }
204+
205+ bool HtmlResource::is_shipped () const { return m_impl->is_shipped (); }
206+
207+ bool HtmlResource::is_external () const { return m_impl->is_external (); }
208+
209+ bool HtmlResource::is_accessible () const { return m_impl->is_accessible (); }
210+
211+ void HtmlResource::write_resource (std::ostream &os) const {
212+ m_impl->write_resource (os);
213+ }
214+
51215HtmlService html::translate (const DecodedFile &decoded_file,
52216 const std::string &output_path,
53217 const HtmlConfig &config) {
0 commit comments