@@ -163,13 +163,41 @@ class HtmlFragmentBase {
163163 Document m_document;
164164};
165165
166+ class HtmlFragmentView final : public HtmlView {
167+ public:
168+ HtmlFragmentView (const abstract::HtmlService &service, std::string name,
169+ std::string path, std::shared_ptr<HtmlFragmentBase> fragment)
170+ : HtmlView(service, std::move(name), std::move(path)),
171+ m_fragment{std::move (fragment)} {}
172+
173+ HtmlResources write_html (html::HtmlWriter &out) const final {
174+ HtmlResources resources;
175+ WritingState state (out, service ().config (), service ().resource_locator (),
176+ resources);
177+ m_fragment->write_document (out, state);
178+ return resources;
179+ }
180+
181+ private:
182+ std::shared_ptr<HtmlFragmentBase> m_fragment;
183+ };
184+
166185class HtmlServiceImpl : public HtmlService {
167186public:
168187 HtmlServiceImpl (Document document,
169188 std::vector<std::shared_ptr<HtmlFragmentBase>> fragments,
170189 HtmlConfig config, HtmlResourceLocator resource_locator)
171190 : HtmlService(std::move(config), std::move(resource_locator)),
172- m_document{std::move (document)}, m_fragments{std::move (fragments)} {}
191+ m_document{std::move (document)}, m_fragments{std::move (fragments)} {
192+ m_views.emplace_back (
193+ std::make_shared<HtmlView>(*this , " document" , " document.html" ));
194+ for (const auto &fragment : m_fragments) {
195+ m_views.emplace_back (std::make_shared<HtmlFragmentView>(
196+ *this , fragment->name (), fragment->name () + " .html" , fragment));
197+ }
198+ }
199+
200+ const HtmlViews &list_views () const final { return m_views; }
173201
174202 [[nodiscard]] Document document () const { return m_document; }
175203
@@ -191,7 +219,9 @@ class HtmlServiceImpl : public HtmlService {
191219 }
192220
193221 bool exists (const std::string &path) const final {
194- if (path == " document.html" ) {
222+ if (std::ranges::any_of (m_views, [&path](const auto &view) {
223+ return view.path () == path;
224+ })) {
195225 return true ;
196226 }
197227
@@ -206,7 +236,9 @@ class HtmlServiceImpl : public HtmlService {
206236 }
207237
208238 std::string mimetype (const std::string &path) const final {
209- if (path == " document.html" ) {
239+ if (std::ranges::any_of (m_views, [&path](const auto &view) {
240+ return view.path () == path;
241+ })) {
210242 return " text/html" ;
211243 }
212244
@@ -220,10 +252,12 @@ class HtmlServiceImpl : public HtmlService {
220252 }
221253
222254 void write (const std::string &path, std::ostream &out) const final {
223- if (path == " document.html" ) {
224- HtmlWriter writer (out, config ());
225- write_document (writer);
226- return ;
255+ for (const auto &view : m_views) {
256+ if (view.path () == path) {
257+ HtmlWriter writer (out, config ());
258+ write_html (path, writer);
259+ return ;
260+ }
227261 }
228262
229263 warmup ();
@@ -244,6 +278,12 @@ class HtmlServiceImpl : public HtmlService {
244278 return write_document (out);
245279 }
246280
281+ for (const auto &view : m_views) {
282+ if (view.path () == path) {
283+ return view.impl ()->write_html (out);
284+ }
285+ }
286+
247287 throw FileNotFound (" Unknown path: " + path);
248288 }
249289
@@ -264,6 +304,9 @@ class HtmlServiceImpl : public HtmlService {
264304protected:
265305 Document m_document;
266306 std::vector<std::shared_ptr<HtmlFragmentBase>> m_fragments;
307+
308+ HtmlViews m_views;
309+
267310 mutable bool m_warm = false ;
268311 mutable HtmlResources m_resources;
269312};
0 commit comments