btool
A parser/converter/transpiler for .bib files
HtmlGenerator.cpp
Go to the documentation of this file.
1 #include <HtmlGenerator.hpp>
2 #include <string>
3 
8 auto HtmlGenerator::fail(const std::string &message, const std::string &type) -> void {
9  AbstractGenerator::fail(message, "HTML");
10 }
11 
17 auto HtmlGenerator::write() -> std::string {
18  if (elements.empty()) {
19  fail("Empty-Input is not Writable", "");
20  }
21  auto document = CTML::Document();
22  document.head()
23  .AppendChild(
24  CTML::Node(name<HtmlTag::META>)
25  .SetAttribute("charset", "utf-8")
26  .UseClosingTag(false)
27  )
28  .AppendChild(
29  CTML::Node(name<HtmlTag::META>)
30  .SetAttribute("name", "viewport")
31  .SetAttribute("content", "width=device-width, initial-scale=1, shrink-to-fit=no")
32  .UseClosingTag(false)
33  )
34  .AppendChild(
35  CTML::Node(name<HtmlTag::LINK>)
36  .SetAttribute("rel", "stylesheet")
37  .SetAttribute("href", "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css")
38  .SetAttribute("integrity", "sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm")
39  .SetAttribute("crossorigin", "anonymous")
40  .UseClosingTag(false)
41  )
42  .AppendChild(
43  CTML::Node(name<HtmlTag::TITLE>, "Bib-Parser results")
44  );
45 
46  auto container = CTML::Node(name<HtmlTag::DIV>).ToggleClass("container");
47 
48  fillContainer(container);
49 
50  document.body()
51  .AppendChild(container)
52  .AppendChild(
53  CTML::Node(name<HtmlTag::SCRIPT>)
54  .SetAttribute("src", "https://code.jquery.com/jquery-3.2.1.slim.min.js")
55  .SetAttribute("integrity", "sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN")
56  .SetAttribute("crossorigin", "anonymous")
57  )
58  .AppendChild(
59  CTML::Node(name<HtmlTag::SCRIPT>)
60  .SetAttribute("src", "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js")
61  .SetAttribute("integrity", "sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q")
62  .SetAttribute("crossorigin", "anonymous")
63  )
64  .AppendChild(
65  CTML::Node(name<HtmlTag::SCRIPT>)
66  .SetAttribute("src", "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js")
67  .SetAttribute("integrity", "sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl")
68  .SetAttribute("crossorigin", "anonymous")
69  );
70 
71  return document.ToString();
72 }
73 
78 HtmlGenerator::HtmlGenerator(const std::vector<BibElement> &elements) : AbstractGenerator{elements} {}
79 
85  CTML::Node &parent
86 ) const noexcept {
87  auto accordion = CTML::Node(name<HtmlTag::DIV>).SetAttribute("id", "accordion");
88  for (const auto &element: elements) {
89  appendCard(accordion, element);
90  }
91  parent.AppendChild(accordion);
92 }
93 
100  CTML::Node &parent,
101  const BibElement &element
102 ) noexcept -> void {
103  auto card = CTML::Node(name<HtmlTag::DIV>).ToggleClass("card");
104 
105  auto cardHeader = CTML::Node(name<HtmlTag::DIV>)
106  .ToggleClass("card-header")
107  .SetAttribute("id", element.id);
108 
109  auto headerHeading = CTML::Node(name<HtmlTag::H5>).ToggleClass("mb-0");
110 
111  auto button = CTML::Node(
112  name<HtmlTag::BUTTON>,
113  element.id + " - " + element.findAttribute("title").value_or<Field>({"", ""}).value
114  )
115  .ToggleClass("btn btn-link collapsed")
116  .SetAttribute("style", "white-space: normal; text-align: left;")
117  .SetAttribute("data-toggle", "collapse")
118  .SetAttribute("data-target", "#collapse" + element.id)
119  .SetAttribute("aria-expanded", "false")
120  .SetAttribute("aria-controls", "collapse" + element.id);
121 
122  auto cardBodyWrapper = CTML::Node(name<HtmlTag::DIV>)
123  .ToggleClass("collapse")
124  .SetAttribute("id", "collapse" + element.id)
125  .SetAttribute("aria-labelledby", element.id)
126  .SetAttribute("data-parent", "#accordion");
127 
128  auto cardBody = CTML::Node(name<HtmlTag::DIV>).ToggleClass("card-body");
129 
130  appendTable(cardBody, element);
131 
132  cardBodyWrapper.AppendChild(cardBody);
133  headerHeading.AppendChild(button);
134  cardHeader.AppendChild(headerHeading);
135  card.AppendChild(cardHeader);
136  card.AppendChild(cardBodyWrapper);
137  parent.AppendChild(card);
138 }
139 
146  CTML::Node &parent,
147  const BibElement &element
148 ) noexcept -> void {
149  auto table = CTML::Node(name<HtmlTag::TABLE>).ToggleClass("table");
150  auto tableBody = CTML::Node(name<HtmlTag::TABLE_BODY>);
151  for (const auto &field : sortedFields(element.attributes)) {
152  auto row = CTML::Node(name<HtmlTag::TABLE_ROW>)
153  .AppendChild(
154  CTML::Node(
155  name<HtmlTag::TABLE_HEADING>,
156  field.name
157  ).SetAttribute("scope", "row")
158  )
159  .AppendChild(CTML::Node(
160  name<HtmlTag::TABLE_ENTRY>,
161  field.value
162  ));
163  tableBody.AppendChild(row);
164  }
165 
166  table.AppendChild(tableBody);
167  parent.AppendChild(table);
168 }
AbstractGenerator
Definition: AbstractGenerator.hpp:14
HtmlGenerator::appendTable
static auto appendTable(CTML::Node &parent, const BibElement &element) noexcept -> void
Definition: HtmlGenerator.cpp:145
AbstractGenerator::fail
virtual auto fail(const std::string &message, const std::string &type="unknown") -> void
Definition: AbstractGenerator.cpp:12
HtmlGenerator::fail
auto fail(const std::string &message, const std::string &type) -> void override
Definition: HtmlGenerator.cpp:8
Field
Field-Container.
Definition: Field.hpp:11
HtmlGenerator::fillContainer
auto fillContainer(CTML::Node &parent) const noexcept -> void
Definition: HtmlGenerator.cpp:84
HtmlGenerator::write
auto write() -> std::string override
Definition: HtmlGenerator.cpp:17
HtmlGenerator.hpp
Field::value
std::string value
the value of the Field
Definition: Field.hpp:13
HtmlGenerator::HtmlGenerator
HtmlGenerator(const std::vector< BibElement > &elements)
Definition: HtmlGenerator.cpp:78
HtmlGenerator::appendCard
static auto appendCard(CTML::Node &parent, const BibElement &element) noexcept -> void
Definition: HtmlGenerator.cpp:99
AbstractGenerator::elements
std::vector< BibElement > elements
Elements to write.
Definition: AbstractGenerator.hpp:16
BibElement
bib-element-Container
Definition: BibElement.hpp:14