statum-mkpstm.vala 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. using Xml;
  2. using Html;
  3. using Astralis;
  4. using Invercargill;
  5. using Invercargill.DataStructures;
  6. namespace Statum.Tools {
  7. /**
  8. * `statum-mkpstm` — the build-time Statum pre-rendering pipeline.
  9. *
  10. * Turns an app HTML page (plus its templates/fragments) into a Vala
  11. * {@link Statum.StatumPage} subclass: a static, precompressed document whose
  12. * route comes from its `<pstm-uri>`. Composition (templates, fragments,
  13. * `pstm-res` rewriting) is applied, then the result is precompressed with
  14. * identity/gzip/zstd/br.
  15. */
  16. public class Mkpstm : Object {
  17. private static string? output_file = null;
  18. private static string? namespace_name = null;
  19. private static string? class_name_override = null;
  20. private static string? route_override = null;
  21. private static List<string> template_dirs = new List<string>();
  22. private Dictionary<string, Xml.Doc*> templates_by_name = new Dictionary<string, Xml.Doc*>();
  23. private Dictionary<string, Xml.Doc*> fragments_by_name = new Dictionary<string, Xml.Doc*>();
  24. private const OptionEntry[] options = {
  25. { "output", 'o', 0, OptionArg.FILENAME, ref output_file, "Output Vala file name", "FILE" },
  26. { "ns", '\0', 0, OptionArg.STRING, ref namespace_name, "Namespace for the generated class", "NAMESPACE" },
  27. { "name", 'n', 0, OptionArg.STRING, ref class_name_override, "Generated class name (default: <InputName>Page)", "NAME" },
  28. { "route", 'r', 0, OptionArg.STRING, ref route_override, "Page route (default: from <pstm-uri>)", "ROUTE" },
  29. { "template-dir", 't', 0, OptionArg.FILENAME_ARRAY, ref template_dirs, "Directory to search for templates/fragments (repeatable)", "DIR" },
  30. { null }
  31. };
  32. public static int main(string[] args) {
  33. try {
  34. var opt = new OptionContext("PAGE_FILE - Generate a Statum StatumPage");
  35. opt.add_main_entries(options, null);
  36. opt.parse(ref args);
  37. } catch (OptionError e) {
  38. stderr.printf("Error: %s\n", e.message);
  39. return 1;
  40. }
  41. string? page_file = null;
  42. if (args.length > 1) {
  43. page_file = args[1];
  44. }
  45. if (page_file == null) {
  46. stderr.printf("Error: No page file specified.\n");
  47. return 1;
  48. }
  49. try {
  50. var tool = new Mkpstm();
  51. var generated = tool.run((!)page_file);
  52. var out_path = tool.output_file ?? (make_pascal_case((!)page_file) + "Page.vala");
  53. write_file(out_path, generated);
  54. stdout.printf("Generated: %s\n", out_path);
  55. return 0;
  56. } catch (GLib.Error e) {
  57. stderr.printf("Error: %s\n", e.message);
  58. return 1;
  59. }
  60. }
  61. public string run(string page_file) throws GLib.Error {
  62. if (template_dirs.length() == 0) {
  63. var parent = File.new_for_path(page_file).get_parent();
  64. template_dirs.append(parent != null ? ((!)parent).get_path() : ".");
  65. }
  66. load_templates_and_fragments(page_file);
  67. var doc = parse_html_file(page_file);
  68. validate_document(doc, page_file);
  69. validate_head_directives(doc, page_file);
  70. // Capture the page's routes before composition (the <pstm-uri>
  71. // directive is dropped while merging templates).
  72. var routes = read_routes(doc);
  73. doc = compose_templates(doc);
  74. inline_fragments(doc);
  75. rewrite_resources(doc);
  76. var variant = build_variant(doc);
  77. var class_name = class_name_override ?? (make_pascal_case(page_file) + "Page");
  78. var route = route_override ?? (routes.length > 0 ? routes[0] : "");
  79. return emit(class_name, route, variant);
  80. }
  81. // ------------------------------------------------------------------
  82. // Template/fragment loading
  83. // ------------------------------------------------------------------
  84. private void load_templates_and_fragments(string page_file) throws GLib.Error {
  85. foreach (var dir in template_dirs) {
  86. var dir_file = File.new_for_path(dir);
  87. if (!dir_file.query_exists()) {
  88. continue;
  89. }
  90. var enumerator = dir_file.enumerate_children("standard::name", 0);
  91. FileInfo info;
  92. while ((info = enumerator.next_file()) != null) {
  93. var name = info.get_name();
  94. if (!name.has_suffix(".html") || name.has_prefix(".")) {
  95. continue;
  96. }
  97. var path = Path.build_filename(dir, name);
  98. if (path == page_file) {
  99. continue;
  100. }
  101. var tdoc = parse_html_file(path);
  102. var source = Path.build_filename(dir, name);
  103. validate_document(tdoc, source);
  104. validate_head_directives(tdoc, source);
  105. var tname = read_pstm_name(tdoc);
  106. if (tname == null) {
  107. continue;
  108. }
  109. if (find_first_element(tdoc->get_root_element(), "pstm-content") != null) {
  110. templates_by_name.set((!)tname, tdoc);
  111. } else {
  112. fragments_by_name.set((!)tname, tdoc);
  113. }
  114. }
  115. }
  116. }
  117. // ------------------------------------------------------------------
  118. // Composition
  119. // ------------------------------------------------------------------
  120. private Xml.Doc* compose_templates(Xml.Doc* doc) throws GLib.Error {
  121. Xml.Doc* current_doc = doc;
  122. string merged_title = read_directive_in_doc(current_doc, "pstm-title") ?? "";
  123. // Guard against cyclic template chains (A templates B templates A).
  124. var visited = new HashSet<string>();
  125. while (true) {
  126. var parent_name = read_directive_in_doc(current_doc, "pstm-template");
  127. if (parent_name == null || parent_name.length == 0) {
  128. break;
  129. }
  130. if (visited.contains((!)parent_name)) {
  131. break;
  132. }
  133. visited.add((!)parent_name);
  134. Xml.Doc* parent_doc;
  135. if (!templates_by_name.try_get((!)parent_name, out parent_doc)) {
  136. throw new IOError.FAILED(@"Unknown template \"$((!)parent_name)\"");
  137. }
  138. // Merge this document's <head> into the parent's <head> (so
  139. // pages, templates and nested components can all declare head
  140. // tags), then splice its <body> into the parent's content outlet.
  141. merge_head(current_doc, parent_doc);
  142. splice_body(current_doc, parent_doc);
  143. var parent_title = read_directive_in_doc(parent_doc, "pstm-title");
  144. if (parent_title != null && parent_title.length > 0) {
  145. merged_title = merged_title + (!)parent_title;
  146. }
  147. current_doc = parent_doc;
  148. }
  149. apply_title(current_doc, merged_title);
  150. return current_doc;
  151. }
  152. /**
  153. * Copies the children of `child_doc`'s <head> into `parent_doc`'s <head>,
  154. * skipping the consumed structural directives (pstm-template/name/title/
  155. * uri) which are page-level metadata and must not be carried up.
  156. */
  157. private void merge_head(Xml.Doc* child_doc, Xml.Doc* parent_doc) {
  158. var child_head = find_head(child_doc);
  159. var parent_head = find_head(parent_doc);
  160. if (child_head == null || parent_head == null) {
  161. return;
  162. }
  163. for (var child = child_head->children; child != null; child = child->next) {
  164. if (!is_element(child)) {
  165. continue;
  166. }
  167. var name = (string) child->name;
  168. if (name == "pstm-template" || name == "pstm-name" || name == "pstm-title" || name == "pstm-uri") {
  169. continue;
  170. }
  171. var copied = child->doc_copy(parent_doc, 1);
  172. if (copied != null) {
  173. parent_head->add_child(copied);
  174. }
  175. }
  176. }
  177. /**
  178. * Splices `child_doc`'s <body> into `parent_doc`'s <pstm-content> outlet
  179. * (or appends to the parent's <body> when there is no outlet). When the
  180. * child <body> carries `pstm-materialise-as`, it is wrapped in an element
  181. * of that tag (carrying the body's non-pstm attributes); otherwise its
  182. * children are spliced directly.
  183. */
  184. private void splice_body(Xml.Doc* child_doc, Xml.Doc* parent_doc) {
  185. var child_body = find_body(child_doc);
  186. var parent_body = find_body(parent_doc);
  187. if (child_body == null || parent_body == null) {
  188. return;
  189. }
  190. var outlet = find_first_element(parent_body, "pstm-content");
  191. var materialise_as = child_body->get_prop("pstm-materialise-as");
  192. if (materialise_as != null) {
  193. var wrapper = parent_body->new_child(null, (!)materialise_as, null);
  194. wrapper->unlink();
  195. copy_non_pstm_attrs(child_body, wrapper);
  196. for (var child = child_body->children; child != null; child = child->next) {
  197. if (is_structural_directive(child)) {
  198. continue;
  199. }
  200. var copied = child->doc_copy(parent_doc, 1);
  201. if (copied != null) {
  202. wrapper->add_child(copied);
  203. }
  204. }
  205. if (outlet != null) {
  206. outlet->add_prev_sibling(wrapper);
  207. } else {
  208. parent_body->add_child(wrapper);
  209. }
  210. } else {
  211. for (var child = child_body->children; child != null; child = child->next) {
  212. if (is_structural_directive(child)) {
  213. continue;
  214. }
  215. var copied = child->doc_copy(parent_doc, 1);
  216. if (copied == null) {
  217. continue;
  218. }
  219. if (outlet != null) {
  220. outlet->add_prev_sibling(copied);
  221. } else {
  222. parent_body->add_child(copied);
  223. }
  224. }
  225. }
  226. if (outlet != null) {
  227. outlet->unlink();
  228. }
  229. }
  230. /** Copies an element's non-`pstm-*` attributes onto another element. */
  231. private static void copy_non_pstm_attrs(Xml.Node* src, Xml.Node* dst) {
  232. for (var attr = src->properties; attr != null; attr = attr->next) {
  233. var attr_name = (string) attr->name;
  234. if (attr_name.has_prefix("pstm-")) {
  235. continue;
  236. }
  237. var value = src->get_prop(attr_name);
  238. if (value != null) {
  239. dst->set_prop(attr_name, (!)value);
  240. }
  241. }
  242. }
  243. private static string? read_directive_in_doc(Xml.Doc* doc, string name) {
  244. var root = doc->get_root_element();
  245. if (root == null) {
  246. return null;
  247. }
  248. return read_directive_text(root, name);
  249. }
  250. private static bool is_structural_directive(Xml.Node* node) {
  251. if (!is_element(node)) {
  252. return false;
  253. }
  254. var name = (string) node->name;
  255. return name == "pstm-template" || name == "pstm-name" || name == "pstm-title" || name == "pstm-uri" || name == "pstm-content";
  256. }
  257. /** Fragment-body metadata elements skipped when inlining (e.g. pstm-input). */
  258. private static bool is_fragment_metadata(Xml.Node* node) {
  259. if (!is_element(node)) {
  260. return false;
  261. }
  262. var name = (string) node->name;
  263. return name == "pstm-input" || name.has_prefix("pstm-");
  264. }
  265. private void inline_fragments(Xml.Doc* doc) {
  266. bool changed = true;
  267. while (changed) {
  268. changed = false;
  269. var includes = find_all_elements(doc, "pstm-fragment");
  270. foreach (var include in includes) {
  271. if (inline_one_fragment(doc, include)) {
  272. changed = true;
  273. }
  274. }
  275. }
  276. }
  277. private bool inline_one_fragment(Xml.Doc* doc, Xml.Node* include) {
  278. var name = include->get_prop("name");
  279. if (name == null) {
  280. return false;
  281. }
  282. Xml.Doc* fragment_doc;
  283. if (!fragments_by_name.try_get((!)name, out fragment_doc)) {
  284. return false;
  285. }
  286. var fragment_body = find_body(fragment_doc);
  287. if (fragment_body == null) {
  288. return false;
  289. }
  290. // Merge the fragment's <head> into the document's <head>.
  291. merge_head(fragment_doc, doc);
  292. var inputs = collect_input_names(fragment_body);
  293. var include_materialise = include->get_prop("pstm-materialise-as");
  294. var fragment_materialise = fragment_body->get_prop("pstm-materialise-as");
  295. var materialise_as = include_materialise ?? fragment_materialise;
  296. bool wrap = (materialise_as != null) || inputs.length > 0;
  297. string tag = materialise_as ?? "div";
  298. if (wrap) {
  299. var parent = include->parent;
  300. if (parent == null) {
  301. return false;
  302. }
  303. var wrapper = parent->new_child(null, tag, null);
  304. wrapper->unlink();
  305. include->add_prev_sibling(wrapper);
  306. // Carry the fragment <body>'s attributes when it supplied the tag.
  307. if (include_materialise == null && fragment_materialise != null) {
  308. copy_non_pstm_attrs(fragment_body, wrapper);
  309. }
  310. for (var child = fragment_body->children; child != null; child = child->next) {
  311. if (is_fragment_metadata(child)) {
  312. continue;
  313. }
  314. var copied = child->doc_copy(doc, 1);
  315. if (copied != null) {
  316. wrapper->add_child(copied);
  317. }
  318. }
  319. foreach (var input_name in inputs) {
  320. var expr = include->get_prop(input_name);
  321. if (expr != null) {
  322. wrapper->set_prop(@"stm-def-$input_name-as", (!)expr);
  323. }
  324. }
  325. } else {
  326. for (var child = fragment_body->children; child != null; child = child->next) {
  327. if (is_fragment_metadata(child)) {
  328. continue;
  329. }
  330. var copied = child->doc_copy(doc, 1);
  331. if (copied != null) {
  332. include->add_prev_sibling(copied);
  333. }
  334. }
  335. }
  336. include->unlink();
  337. return true;
  338. }
  339. private string[] collect_input_names(Xml.Node* fragment_body) {
  340. var names = new string[0];
  341. for (var n = fragment_body->children; n != null; n = n->next) {
  342. if (is_element(n) && (string) n->name == "pstm-input") {
  343. var input_name = n->get_prop("name");
  344. if (input_name != null) {
  345. names += (!)input_name;
  346. }
  347. }
  348. }
  349. return names;
  350. }
  351. private void rewrite_resources(Xml.Doc* doc) {
  352. var root = doc->get_root_element();
  353. if (root != null) {
  354. rewrite_resources_in(root);
  355. }
  356. }
  357. private void rewrite_resources_in(Xml.Node* node) {
  358. if (!is_element(node)) {
  359. return;
  360. }
  361. string[] to_add_attr = new string[0];
  362. string[] to_add_val = new string[0];
  363. string[] to_remove = new string[0];
  364. for (var attr = node->properties; attr != null; attr = attr->next) {
  365. var attr_name = (string) attr->name;
  366. if (attr_name.has_prefix("pstm-res-")) {
  367. var target_attr = attr_name.substring("pstm-res-".length);
  368. var value = node->get_prop(attr_name);
  369. if (value != null) {
  370. to_add_attr += target_attr;
  371. to_add_val += @"/_statum/resource/$((!)value)";
  372. }
  373. to_remove += attr_name;
  374. }
  375. }
  376. for (int i = 0; i < to_add_attr.length; i++) {
  377. node->set_prop(to_add_attr[i], to_add_val[i]);
  378. }
  379. foreach (var attr_name in to_remove) {
  380. node->unset_prop(attr_name);
  381. }
  382. for (var child = node->children; child != null; child = child->next) {
  383. rewrite_resources_in(child);
  384. }
  385. }
  386. // ------------------------------------------------------------------
  387. // Variant rendering
  388. // ------------------------------------------------------------------
  389. /**
  390. * Renders the composed document to its single HTML form (stripping every
  391. * `pstm-*` directive) and precompresses it. Pages no longer have
  392. * variants: a page is a static, precompressed document, so there is
  393. * exactly one.
  394. */
  395. private VariantData build_variant(Xml.Doc* doc) throws GLib.Error {
  396. strip_pstm(doc);
  397. var html = serialize(doc);
  398. return compress_variant(html);
  399. }
  400. private VariantData compress_variant(string html) throws GLib.Error {
  401. var data = new VariantData();
  402. data.identity = html.data;
  403. var buffer = new ByteBuffer.from_byte_array(html.data);
  404. var gzip = new GzipCompressor(9).compress_buffer(buffer, null).to_array();
  405. var zstd = new ZstdCompressor(19).compress_buffer(buffer, null).to_array();
  406. var br = new BrotliCompressor(11).compress_buffer(buffer, null).to_array();
  407. data.has_gzip = gzip.length < html.length;
  408. data.has_zstd = zstd.length < html.length;
  409. data.has_br = br.length < html.length;
  410. data.gzip = data.has_gzip ? gzip : null;
  411. data.zstd = data.has_zstd ? zstd : null;
  412. data.br = data.has_br ? br : null;
  413. return data;
  414. }
  415. /** Remove every `pstm-*` attribute and `pstm-*` element from the tree. */
  416. private void strip_pstm(Xml.Doc* doc) {
  417. var root = doc->get_root_element();
  418. if (root != null) {
  419. strip_pstm_from(root);
  420. }
  421. }
  422. private void strip_pstm_from(Xml.Node* node) {
  423. string[] to_remove = new string[0];
  424. for (var attr = node->properties; attr != null; attr = attr->next) {
  425. var attr_name = (string) attr->name;
  426. if (attr_name.has_prefix("pstm-")) {
  427. to_remove += attr_name;
  428. }
  429. }
  430. foreach (var attr_name in to_remove) {
  431. node->unset_prop(attr_name);
  432. }
  433. var child = node->children;
  434. while (child != null) {
  435. var next = child->next;
  436. if (is_element(child) && ((string) child->name).has_prefix("pstm-")) {
  437. child->unlink();
  438. } else if (is_element(child)) {
  439. strip_pstm_from(child);
  440. }
  441. child = next;
  442. }
  443. }
  444. // ------------------------------------------------------------------
  445. // Code generation
  446. // ------------------------------------------------------------------
  447. private string emit(string class_name, string route, VariantData variant) throws GLib.Error {
  448. var page_hash = compute_hash(variant.identity);
  449. var b = new StringBuilder();
  450. string i1 = namespace_name != null ? " " : "";
  451. string i2 = namespace_name != null ? " " : " ";
  452. string i3 = namespace_name != null ? " " : " ";
  453. b.append("using Statum;\n");
  454. b.append("using Invercargill;\n");
  455. b.append("using Invercargill.DataStructures;\n");
  456. b.append("using Astralis;\n\n");
  457. if (namespace_name != null) {
  458. b.append_printf("namespace %s {\n\n", namespace_name);
  459. }
  460. b.append_printf("%s// Generated by statum-mkpstm\n", i1);
  461. b.append_printf("%spublic class %s : StatumPage {\n\n", i1, class_name);
  462. b.append_printf("%spublic override string route { get { return \"%s\"; } }\n", i2, escape(route));
  463. b.append_printf("%spublic override string content_type { get { return \"text/html; charset=utf-8\"; } }\n\n", i2);
  464. // get_best_encoding: smallest available supported encoding.
  465. b.append_printf("%spublic override string get_best_encoding(Set<string> supported) {\n", i2);
  466. if (variant.has_br) {
  467. b.append_printf("%sif (supported.has(\"br\")) return \"br\";\n", i3);
  468. }
  469. if (variant.has_zstd) {
  470. b.append_printf("%sif (supported.has(\"zstd\")) return \"zstd\";\n", i3);
  471. }
  472. if (variant.has_gzip) {
  473. b.append_printf("%sif (supported.has(\"gzip\")) return \"gzip\";\n", i3);
  474. }
  475. b.append_printf("%sreturn \"identity\";\n", i3);
  476. b.append_printf("%s}\n\n", i2);
  477. // get_encoding
  478. b.append_printf("%spublic override unowned uint8[] get_encoding(string encoding) {\n", i2);
  479. b.append_printf("%sswitch (encoding) {\n", i3);
  480. b.append_printf("%scase \"identity\": return IDENTITY;\n", i3);
  481. if (variant.has_gzip && variant.gzip != null) {
  482. b.append_printf("%scase \"gzip\": return GZIP;\n", i3);
  483. }
  484. if (variant.has_zstd && variant.zstd != null) {
  485. b.append_printf("%scase \"zstd\": return ZSTD;\n", i3);
  486. }
  487. if (variant.has_br && variant.br != null) {
  488. b.append_printf("%scase \"br\": return BR;\n", i3);
  489. }
  490. b.append_printf("%sdefault: return IDENTITY;\n", i3);
  491. b.append_printf("%s}\n", i3);
  492. b.append_printf("%s}\n\n", i2);
  493. // get_etag_for (ETag = "<page-hash>-<encoding>")
  494. b.append_printf("%spublic override string get_etag_for(string encoding) {\n", i2);
  495. b.append_printf("%sreturn \"\\\"%s-%%s\\\"\".printf(encoding);\n", i3, page_hash);
  496. b.append_printf("%s}\n\n", i2);
  497. // Byte arrays.
  498. emit_one_array(b, i2, i3, "IDENTITY", variant.identity);
  499. if (variant.has_gzip && variant.gzip != null) {
  500. emit_one_array(b, i2, i3, "GZIP", (!)variant.gzip);
  501. }
  502. if (variant.has_zstd && variant.zstd != null) {
  503. emit_one_array(b, i2, i3, "ZSTD", (!)variant.zstd);
  504. }
  505. if (variant.has_br && variant.br != null) {
  506. emit_one_array(b, i2, i3, "BR", (!)variant.br);
  507. }
  508. b.append_printf("%s}\n", i1);
  509. if (namespace_name != null) {
  510. b.append("}\n");
  511. }
  512. return b.str;
  513. }
  514. private void emit_one_array(StringBuilder b, string i2, string i3, string name, uint8[] data) {
  515. b.append_printf("%sprivate const uint8[] %s = {\n", i2, name);
  516. for (int i = 0; i < data.length; i++) {
  517. if (i == 0) {
  518. b.append(i3);
  519. } else if (i % 16 == 0) {
  520. b.append(",\n").append(i3);
  521. } else {
  522. b.append(", ");
  523. }
  524. b.append_printf("0x%02x", data[i]);
  525. }
  526. b.append_printf("\n%s};\n\n", i2);
  527. }
  528. // ------------------------------------------------------------------
  529. // DOM helpers
  530. // ------------------------------------------------------------------
  531. private Xml.Doc* parse_html_file(string path) throws GLib.Error {
  532. var file = File.new_for_path(path);
  533. if (!file.query_exists()) {
  534. throw new IOError.NOT_FOUND(@"Page file '$path' does not exist");
  535. }
  536. uint8[] contents;
  537. string etag_out;
  538. file.load_contents(null, out contents, out etag_out);
  539. // Parse with libxml2's HTML parser (mirrors Astralis' MarkupDocument)
  540. // so the tree has HTML semantics and serializes as HTML, not XML.
  541. int opts = (int)(Html.ParserOption.RECOVER |
  542. Html.ParserOption.NOERROR |
  543. Html.ParserOption.NOWARNING |
  544. Html.ParserOption.NOBLANKS |
  545. Html.ParserOption.NONET);
  546. char[] buffer = ((string) contents).to_utf8();
  547. var doc = Html.Doc.read_memory(buffer, buffer.length, path, "utf-8", opts);
  548. if (doc == null) {
  549. throw new IOError.FAILED(@"Failed to parse HTML file '$path'");
  550. }
  551. return doc;
  552. }
  553. private static Xml.Node* find_body(Xml.Doc* doc) {
  554. var root = doc->get_root_element();
  555. if (root == null) {
  556. return null;
  557. }
  558. return find_first_element(root, "body");
  559. }
  560. private static Xml.Node* find_head(Xml.Doc* doc) {
  561. var root = doc->get_root_element();
  562. if (root == null) {
  563. return null;
  564. }
  565. return find_first_element(root, "head");
  566. }
  567. /**
  568. * Validates that `doc` has both a <head> and a <body> element, which every
  569. * Statum HTML input (pages, templates and fragments) must declare.
  570. */
  571. private void validate_document(Xml.Doc* doc, string source) throws GLib.Error {
  572. if (find_head(doc) == null) {
  573. throw new IOError.FAILED(@"\"$source\" has no <head> element (a <head> and <body> are required)");
  574. }
  575. if (find_body(doc) == null) {
  576. throw new IOError.FAILED(@"\"$source\" has no <body> element (a <head> and <body> are required)");
  577. }
  578. }
  579. /**
  580. * Validates that the structural directives <pstm-title>, <pstm-template>
  581. * and <pstm-name> appear only inside <head>.
  582. */
  583. private void validate_head_directives(Xml.Doc* doc, string source) throws GLib.Error {
  584. var root = doc->get_root_element();
  585. if (root == null) {
  586. return;
  587. }
  588. string[] names = { "pstm-title", "pstm-template", "pstm-name", "pstm-uri" };
  589. foreach (var name in names) {
  590. var nodes = new Series<Xml.Node*>();
  591. find_all_from(root, name, nodes);
  592. foreach (var node in nodes) {
  593. if (!is_descendant_of_head(node)) {
  594. throw new IOError.FAILED(@"\"$source\": <$name> must appear inside <head>");
  595. }
  596. }
  597. }
  598. }
  599. /** Walks the ancestor chain to determine whether `node` is within <head>. */
  600. private static bool is_descendant_of_head(Xml.Node* node) {
  601. for (var p = node->parent; p != null; p = p->parent) {
  602. if (is_element(p) && ((string) p->name).down() == "head") {
  603. return true;
  604. }
  605. }
  606. return false;
  607. }
  608. private static Xml.Node* find_first_element(Xml.Node* root, string name) {
  609. if (!is_element(root)) {
  610. return null;
  611. }
  612. if (((string) root->name).down() == name.down()) {
  613. return root;
  614. }
  615. for (var child = root->children; child != null; child = child->next) {
  616. var found = find_first_element(child, name);
  617. if (found != null) {
  618. return found;
  619. }
  620. }
  621. return null;
  622. }
  623. private static Xml.Node*[] find_all_elements(Xml.Doc* doc, string name) {
  624. var collected = new Series<Xml.Node*>();
  625. var root = doc->get_root_element();
  626. if (root != null) {
  627. find_all_from(root, name, collected);
  628. }
  629. return collected.to_array();
  630. }
  631. private static void find_all_from(Xml.Node* node, string name, Series<Xml.Node*> result) {
  632. if (is_element(node) && ((string) node->name).down() == name.down()) {
  633. result.add(node);
  634. }
  635. if (is_element(node)) {
  636. for (var child = node->children; child != null; child = child->next) {
  637. find_all_from(child, name, result);
  638. }
  639. }
  640. }
  641. private static string? read_directive_text(Xml.Node* body, string directive) {
  642. var node = find_first_element(body, directive);
  643. if (node == null) {
  644. return null;
  645. }
  646. var text = element_text(node);
  647. return text.strip().length > 0 ? text.strip() : null;
  648. }
  649. private static string? read_pstm_name(Xml.Doc* doc) {
  650. var node = find_first_element(doc->get_root_element(), "pstm-name");
  651. if (node == null) {
  652. return null;
  653. }
  654. return element_text(node).strip();
  655. }
  656. private static string[] read_routes(Xml.Doc* doc) {
  657. string[] routes = new string[0];
  658. var nodes = find_all_elements(doc, "pstm-uri");
  659. foreach (var node in nodes) {
  660. var text = element_text(node).strip();
  661. if (text.length > 0) {
  662. routes += text;
  663. }
  664. }
  665. return routes;
  666. }
  667. private static string element_text(Xml.Node* node) {
  668. var b = new StringBuilder();
  669. for (var child = node->children; child != null; child = child->next) {
  670. if (child->type == ElementType.TEXT_NODE && child->content != null) {
  671. b.append((string) child->content);
  672. } else if (is_element(child)) {
  673. b.append(element_text(child));
  674. }
  675. }
  676. return b.str;
  677. }
  678. private static void apply_title(Xml.Doc* doc, string? title) {
  679. if (title == null || title.length == 0) {
  680. return;
  681. }
  682. var head = find_first_element(doc->get_root_element(), "head");
  683. if (head == null) {
  684. return;
  685. }
  686. var title_node = find_first_element(head, "title");
  687. if (title_node == null) {
  688. title_node = head->new_child(null, "title", null);
  689. }
  690. for (var child = title_node->children; child != null; child = child->next) {
  691. child->unlink();
  692. }
  693. title_node->add_content(title);
  694. }
  695. private static string serialize(Xml.Doc* doc) {
  696. // Serialize with libxml2's HTML serializer (htmlDocDumpMemory) so the
  697. // output is valid HTML: no <?xml?> prolog, no self-closing
  698. // `<script/>`, and correct void/non-void element handling. The doc was
  699. // produced by the HTML parser, so it carries HTML structure.
  700. string mem;
  701. int len;
  702. ((Html.Doc*) doc)->dump_memory(out mem, out len);
  703. return len > 0 ? mem.substring(0, len) : mem;
  704. }
  705. private static bool is_element(Xml.Node* node) {
  706. return node != null && node->type == ElementType.ELEMENT_NODE;
  707. }
  708. private static string compute_hash(uint8[] data) {
  709. var checksum = new Checksum(ChecksumType.SHA512);
  710. checksum.update(data, data.length);
  711. return checksum.get_string();
  712. }
  713. private static string escape(string s) {
  714. return s.replace("\\", "\\\\").replace("\"", "\\\"");
  715. }
  716. private static void write_file(string path, string contents) throws GLib.Error {
  717. var file = File.new_for_path(path);
  718. var stream = new DataOutputStream(file.replace(null, false, FileCreateFlags.NONE));
  719. stream.put_string(contents);
  720. stream.close();
  721. }
  722. private static string make_pascal_case(string input) {
  723. var basename = Path.get_basename(input);
  724. var dot = basename.last_index_of(".");
  725. if (dot > 0) {
  726. basename = basename.substring(0, dot);
  727. }
  728. var result = new StringBuilder();
  729. var capitalize_next = true;
  730. foreach (var c in basename.data) {
  731. if (c == '-' || c == '_' || c == ' ' || c == '.') {
  732. capitalize_next = true;
  733. } else {
  734. if (capitalize_next) {
  735. result.append_printf("%c", c >= 'a' && c <= 'z' ? c - 32 : c);
  736. capitalize_next = false;
  737. } else {
  738. result.append_printf("%c", c);
  739. }
  740. }
  741. }
  742. return result.str;
  743. }
  744. // ------------------------------------------------------------------
  745. // Model
  746. // ------------------------------------------------------------------
  747. private class VariantData : Object {
  748. public uint8[] identity;
  749. public uint8[]? gzip;
  750. public uint8[]? zstd;
  751. public uint8[]? br;
  752. public bool has_gzip;
  753. public bool has_zstd;
  754. public bool has_br;
  755. }
  756. }
  757. }