1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
//! HTML Converter
//!
//! Converts the Org AST to its HTML representation.
use org_rust_parser as org_parser;
use core::fmt;
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::fmt::{Result, Write};
use latex2mathml::{latex_to_mathml, DisplayStyle};
use memchr::memchr3_iter;
use org_parser::element::{Affiliated, Block, CheckBox, ListKind, TableRow};
use org_parser::parse_macro_call;
use org_parser::types::{Expr, Node, Parser};
use crate::org_macros::macro_handle;
use crate::types::Exporter;
use org_parser::node_pool::NodeID;
use org_parser::object::{LatexFragment, PathReg, PlainOrRec};
use org_parser::parse_org;
use phf::phf_set;
const BACKEND_NAME: &str = "html";
// file types we can wrap an `img` around
static IMAGE_TYPES: phf::Set<&str> = phf_set! {
"jpeg",
"jpg",
"png",
"gif",
"svg",
"webp",
};
pub struct Html<'buf> {
buf: &'buf mut dyn fmt::Write,
// HACK: When we export a caption, insert the child id here to make sure
// it's not double exported
nox: HashSet<NodeID>, // no-export
// used footnotes
footnotes: Vec<NodeID>,
footnote_ids: HashMap<NodeID, usize>,
}
/// Wrapper around strings that need to be properly HTML escaped.
pub(crate) struct HtmlEscape<'a>(pub &'a str);
impl<'a> fmt::Display for HtmlEscape<'a> {
// design based on:
// https://lise-henry.github.io/articles/optimising_strings.html
// we can iterate over bytes since it's not possible for
// an ascii character to appear in the codepoint of another larger char
// if we see an ascii, then it's guaranteed to be valid
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result {
let mut prev_pos = 0;
// there are other characters we could escape, but memchr caps out at 3
// the really important one is `<`, and then also probably &
// throwing in `>` for good measure
// based on:
// https://mina86.com/2021/no-you-dont-need-to-escape-that/
// there are invariants in the parsing (i hope) that should make
// using memchr3 okay. if not, consider using jetscii for more byte blasting
let escape_bytes = memchr3_iter(b'<', b'&', b'>', self.0.as_bytes());
for ret in escape_bytes {
write!(f, "{}", &self.0[prev_pos..ret])?;
match self.0.as_bytes()[ret] {
b'<' => write!(f, r"<")?,
b'>' => write!(f, r">")?,
b'&' => write!(f, r"&")?,
_ => unreachable!(),
}
prev_pos = ret + 1;
}
write!(f, "{}", &self.0[prev_pos..])
}
}
impl<'buf> Exporter<'buf> for Html<'buf> {
fn export(input: &str) -> core::result::Result<String, fmt::Error> {
let mut buf = String::new();
Html::export_buf(input, &mut buf)?;
Ok(buf)
}
fn export_buf<'inp, T: fmt::Write>(
input: &'inp str,
buf: &'buf mut T,
) -> core::result::Result<&'buf mut T, fmt::Error> {
let parsed = parse_org(input);
let mut obj = Html {
buf,
nox: HashSet::new(),
footnotes: Vec::new(),
footnote_ids: HashMap::new(),
};
obj.export_rec(&parsed.pool.root_id(), &parsed)?;
obj.exp_footnotes(&parsed)?;
Ok(buf)
}
fn export_macro_buf<'inp, T: fmt::Write>(
input: &'inp str,
buf: &'buf mut T,
) -> core::result::Result<&'buf mut T, fmt::Error> {
let parsed = parse_macro_call(input);
let mut obj = Html {
buf,
nox: HashSet::new(),
footnotes: Vec::new(),
footnote_ids: HashMap::new(),
};
obj.export_rec(&parsed.pool.root_id(), &parsed)?;
Ok(buf)
}
fn export_rec(&mut self, node_id: &NodeID, parser: &Parser) -> Result {
// avoid parsing this node
if self.nox.contains(node_id) {
return Ok(());
}
let node = &parser.pool[*node_id];
match &node.obj {
Expr::Root(inner) => {
for id in inner {
self.export_rec(id, parser)?;
}
}
Expr::Heading(inner) => {
let heading_number: u8 = inner.heading_level.into();
write!(self, "<h{heading_number}",)?;
self.prop(node)?;
write!(self, ">")?;
if let Some(title) = &inner.title {
for id in &title.1 {
self.export_rec(id, parser)?;
}
}
writeln!(self, "</h{heading_number}>")?;
if let Some(children) = &inner.children {
for id in children {
self.export_rec(id, parser)?;
}
}
}
Expr::Block(inner) => {
match inner {
// Greater Blocks
Block::Center {
parameters: _,
contents,
} => {
write!(self, "<div")?;
self.class("center")?;
self.prop(node)?;
writeln!(self, ">")?;
for id in contents {
self.export_rec(id, parser)?;
}
writeln!(self, "</div>")?;
}
Block::Quote {
parameters: _,
contents,
} => {
write!(self, "<blockquote")?;
self.prop(node)?;
writeln!(self, ">")?;
for id in contents {
self.export_rec(id, parser)?;
}
writeln!(self, "</blockquote>")?;
}
Block::Special {
parameters: _,
contents,
name,
} => {
write!(self, "<div")?;
self.prop(node)?;
self.class(name)?;
writeln!(self, ">")?;
for id in contents {
self.export_rec(id, parser)?;
}
writeln!(self, "</div>")?;
}
// Lesser blocks
Block::Comment {
parameters: _,
contents,
} => {
writeln!(self, "<!--{contents}-->")?;
}
Block::Example {
parameters: _,
contents,
} => {
write!(self, "<pre")?;
self.class("example")?;
self.prop(node)?;
writeln!(self, ">\n{}</pre>", HtmlEscape(contents))?;
}
Block::Export {
parameters,
contents,
} => {
if let Some(params) = parameters {
if params.contains(BACKEND_NAME) {
writeln!(self, "{contents}")?;
}
}
}
Block::Src {
parameters: _,
contents,
} => {
// TODO: work with the language parameter
write!(self, "<pre")?;
self.class("src")?;
self.prop(node)?;
writeln!(self, ">\n{}</pre>", HtmlEscape(contents))?;
}
Block::Verse {
parameters: _,
contents,
} => {
// FIXME: apparently verse blocks contain objects...
write!(self, "<p")?;
self.class("verse")?;
self.prop(node)?;
writeln!(self, ">\n{}</p>", HtmlEscape(contents))?;
}
}
}
Expr::RegularLink(inner) => {
let path_link: String = match inner.path.obj {
PathReg::PlainLink(a) => a.into(),
PathReg::Id(a) => format!("#{a}"),
PathReg::CustomId(a) => format!("#{a}"),
PathReg::Coderef(_) => todo!(),
PathReg::Unspecified(a) => {
let mut rita = String::new();
for (match_targ, ret) in parser.targets.iter() {
if match_targ.starts_with(a) {
rita = format!("#{ret}");
break;
}
}
// TODO: how to handle non-existing links
rita
}
};
write!(self, r#"<a href="{}">"#, HtmlEscape(&path_link))?;
if let Some(children) = &inner.description {
for id in children {
self.export_rec(id, parser)?;
}
} else {
write!(self, "{}", HtmlEscape(inner.path.to_str(parser.source)))?;
}
write!(self, "</a>")?;
}
Expr::Paragraph(inner) => {
if inner.0.len() == 1 {
if let Expr::RegularLink(link) = &parser.pool[inner.0[0]].obj {
let link_source = link.path.to_str(parser.source);
let ending_tag = link_source.split('.').last();
if let Some(extension) = ending_tag {
if IMAGE_TYPES.contains(extension) {
write!(self, "<figure>\n<img")?;
self.prop(node)?;
write!(self, r#" src="{}""#, HtmlEscape(link_source))?;
write!(self, r#" alt=""#)?;
if let Some(children) = &link.description {
for id in children {
self.export_rec(id, parser)?;
}
} else {
let alt_text =
if let Some(slashed) = link_source.split('/').last() {
slashed
} else {
link_source
};
write!(self, "{}", HtmlEscape(alt_text))?;
}
write!(self, "\">\n</figure>")?;
return Ok(());
}
}
}
}
write!(self, "<p")?;
self.prop(node)?;
writeln!(self, ">")?;
for id in &inner.0 {
self.export_rec(id, parser)?;
}
writeln!(self, "\n</p>")?;
}
Expr::Italic(inner) => {
write!(self, "<em>")?;
for id in &inner.0 {
self.export_rec(id, parser)?;
}
write!(self, "</em>")?;
}
Expr::Bold(inner) => {
write!(self, "<b>")?;
for id in &inner.0 {
self.export_rec(id, parser)?;
}
write!(self, "</b>")?;
}
Expr::StrikeThrough(inner) => {
write!(self, "<del>")?;
for id in &inner.0 {
self.export_rec(id, parser)?;
}
write!(self, "</del>")?;
}
Expr::Underline(inner) => {
write!(self, "<u>")?;
for id in &inner.0 {
self.export_rec(id, parser)?;
}
write!(self, "</u>")?;
// write!(self, "<span class=underline>")?;
// for id in &inner.0 {
// self.export_rec(id, parser)?;
// }
// write!(self, "</span>")?;
}
Expr::BlankLine => {
// write!(self, "\n")?;
}
Expr::SoftBreak => {
write!(self, " ")?;
}
Expr::LineBreak => {
writeln!(self, "\n<br>")?;
}
Expr::HorizontalRule => {
writeln!(self, "\n<hr>")?;
}
Expr::Plain(inner) => {
write!(self, "{}", HtmlEscape(inner))?;
}
Expr::Verbatim(inner) => {
write!(self, "<code>{}</code>", HtmlEscape(inner.0))?;
}
Expr::Code(inner) => {
write!(self, "<code>{}</code>", HtmlEscape(inner.0))?;
}
Expr::Comment(inner) => {
write!(self, "<!--{}-->", inner.0)?;
}
Expr::InlineSrc(inner) => {
write!(
self,
"<code class={}>{}</code>",
inner.lang,
HtmlEscape(inner.body)
)?;
// if let Some(args) = inner.headers {
// write!(self, "[{args}]")?;
// }
// write!(self, "{{{}}}", inner.body)?;
}
Expr::Keyword(_) => {
// todo!()
// match inner {
// Keyword::Basic { key, val } => {
// if ORG_AFFILIATED_KEYWORDS.contains(key) {
// todo!()
// }
// }
// Keyword::Macro(_) => todo!(),
// Keyword::Affilliated(_) => todo!(),
// }
}
Expr::LatexEnv(inner) => {
let ret = latex_to_mathml(
&format!(
r"\begin{{{0}}}
{1}
\end{{{0}}}
",
inner.name, inner.contents
),
DisplayStyle::Block,
)
.unwrap();
writeln!(self, "{ret}")?;
}
Expr::LatexFragment(inner) => match inner {
LatexFragment::Command { name, contents } => {
let mut pot_cont = String::new();
write!(pot_cont, r#"{name}"#)?;
if let Some(command_cont) = contents {
write!(pot_cont, "{{{command_cont}}}")?;
}
write!(
self,
"{}",
&latex_to_mathml(&pot_cont, DisplayStyle::Inline).unwrap(),
)?;
}
LatexFragment::Display(inner) => {
writeln!(
self,
"{}",
&latex_to_mathml(inner, DisplayStyle::Block).unwrap()
)?;
}
LatexFragment::Inline(inner) => {
write!(
self,
"{}",
&latex_to_mathml(inner, DisplayStyle::Inline).unwrap()
)?;
}
},
Expr::Item(inner) => {
if let Some(tag) = inner.tag {
write!(self, "<dt>{}</dt>", HtmlEscape(tag))?;
write!(self, "<dd>")?;
for id in &inner.children {
self.export_rec(id, parser)?;
}
write!(self, "</dd>")?;
} else {
write!(self, "<li")?;
if let Some(counter) = inner.counter_set {
self.attr("value", counter)?;
}
if let Some(check) = &inner.check_box {
self.class(match check {
CheckBox::Intermediate => "trans",
CheckBox::Off => "off",
CheckBox::On => "on",
})?;
}
write!(self, ">")?;
for id in &inner.children {
self.export_rec(id, parser)?;
}
writeln!(self, "</li>")?;
}
}
Expr::PlainList(inner) => {
let tag = match inner.kind {
ListKind::Unordered => "ul",
ListKind::Ordered(_) => "ol",
ListKind::Descriptive => "dd",
};
write!(self, "<{tag}")?;
self.prop(node)?;
writeln!(self, ">")?;
for id in &inner.children {
self.export_rec(id, parser)?;
}
writeln!(self, "</{tag}>")?;
}
Expr::PlainLink(inner) => {
write!(
self,
"<a href={0}:{1}>{0}:{1}</a>",
inner.protocol, inner.path
)?;
}
Expr::Entity(inner) => {
write!(self, "{}", inner.mapped_item)?;
}
Expr::Table(inner) => {
write!(self, "<table")?;
self.prop(node)?;
writeln!(self, ">")?;
for id in &inner.children {
self.export_rec(id, parser)?;
}
writeln!(self, "</table>")?;
}
Expr::TableRow(inner) => {
match inner {
TableRow::Rule => { /*skip*/ }
TableRow::Standard(stands) => {
writeln!(self, "<tr>")?;
for id in stands.iter() {
self.export_rec(id, parser)?;
}
writeln!(self, "</tr>")?;
}
}
}
Expr::TableCell(inner) => {
write!(self, "<td>")?;
for id in &inner.0 {
self.export_rec(id, parser)?;
}
writeln!(self, "</td>")?;
}
Expr::Emoji(inner) => {
write!(self, "{}", inner.mapped_item)?;
}
Expr::Superscript(inner) => {
write!(self, "<sup>")?;
match &inner.0 {
PlainOrRec::Plain(inner) => {
write!(self, "{inner}")?;
}
PlainOrRec::Rec(inner) => {
for id in inner {
self.export_rec(id, parser)?;
}
}
}
write!(self, "</sup>")?;
}
Expr::Subscript(inner) => {
write!(self, "<sub>")?;
match &inner.0 {
PlainOrRec::Plain(inner) => {
write!(self, "{inner}")?;
}
PlainOrRec::Rec(inner) => {
for id in inner {
self.export_rec(id, parser)?;
}
}
}
write!(self, "</sub>")?;
}
Expr::Target(inner) => {
write!(self, "<span")?;
self.prop(node)?;
write!(self, ">")?;
write!(
self,
"<span id={}>{}</span>",
parser.pool[*node_id].id_target.as_ref().unwrap(), // must exist
HtmlEscape(inner.0)
)?;
}
Expr::Macro(macro_call) => {
if let Ok(macro_contents) = macro_handle(parser, macro_call) {
match macro_contents {
Cow::Owned(p) => {
Html::export_macro_buf(&p, self)?;
}
Cow::Borrowed(r) => {
write!(self, "{}", HtmlEscape(r))?;
}
}
}
}
Expr::Drawer(inner) => {
for id in &inner.children {
self.export_rec(id, parser)?;
}
}
Expr::ExportSnippet(inner) => {
if inner.backend == BACKEND_NAME {
write!(self, "{}", inner.contents)?;
}
}
Expr::Affiliated(inner) => match inner {
Affiliated::Name(_id) => {}
Affiliated::Caption(id, contents) => {
if let Some(caption_id) = id {
writeln!(self, "<figure>")?;
writeln!(self, "<figcaption>{contents}</figcaption>")?;
self.export_rec(caption_id, parser)?;
writeln!(self, "</figure>")?;
self.nox.insert(*caption_id);
}
}
Affiliated::Attr { .. } => {}
},
Expr::MacroDef(_) => {}
Expr::FootnoteDef(_) => {
// handled after root
}
Expr::FootnoteRef(inner) => {
let foot_len = self.footnotes.len();
let target_id = if let Some(label) = inner.label {
if let Some(def_id) = parser.footnotes.get(label) {
*def_id
} else {
*node_id
}
} else {
*node_id
};
let index = *self.footnote_ids.entry(target_id).or_insert_with(|| {
self.footnotes.push(target_id);
foot_len + 1
});
// prevent duplicate ids:
// node ids are guaranteed to be unique
let fn_id = if index != foot_len + 1 {
format!("{index}.{node_id}")
} else {
format!("{index}")
};
write!(
self,
r##"<sup>
<a id="fnr.{0}" href="#fn.{1}" class="footref" role="doc-backlink">{1}</a>
</sup>"##,
fn_id, index,
)?;
}
}
Ok(())
}
}
impl<'buf> Html<'buf> {
fn prop(&mut self, node: &Node) -> Result {
if let Some(tag_contents) = node.id_target.as_ref() {
write!(self, r#" id="{tag_contents}""#)?;
}
if let Some(attrs) = node.attrs.get(BACKEND_NAME) {
for item in attrs {
self.attr(item.key, item.val)?;
}
}
Ok(())
}
fn class(&mut self, name: &str) -> Result {
write!(self, r#" class="{name}""#)
}
fn attr(&mut self, key: &str, val: &str) -> Result {
write!(self, r#" {}="{}""#, key, HtmlEscape(val))
}
fn exp_footnotes(&mut self, parser: &Parser) -> Result {
if self.footnotes.is_empty() {
return Ok(());
}
// get last heading, and check if its title is Footnotes,
// if so, destroy it
let heading_query = parser.pool.iter().rev().find(|node| {
if let Expr::Heading(head) = &node.obj {
if let Some(title) = &head.title {
if title.0 == "Footnotes\n" {
return true;
}
}
}
false
});
writeln!(
self,
r#"
<div id="footnotes">
<style>
.footdef p {{
display:inline;
}}
</style>"#
)?;
if heading_query.is_none() {
writeln!(self, r#" <h2 class="footnotes">Footnotes</h2>"#)?;
}
writeln!(self, r#" <div id="text-footnotes">"#)?;
// FIXME
// lifetime shenanigans making me do this.. can't figure em out
// would like to self.footnotes.iter(), but we get multiple
// immutable borrows, so self.footnotes.copied.iter(), but still no go
let man = self.footnotes.clone();
for (mut pos, def_id) in man.iter().enumerate() {
pos += 1;
write!(
self,
r##"
<div class="footdef">
<sup>
<a id="fn.{pos}" href= "#fnr.{pos}" role="doc-backlink">{pos}</a>
</sup>
"##
)?;
match &parser.pool[*def_id].obj {
Expr::FootnoteDef(fn_def) => {
for child_id in &fn_def.children {
self.export_rec(child_id, parser)?;
}
}
Expr::FootnoteRef(fn_ref) => {
if let Some(children) = fn_ref.children.as_ref() {
for child_id in children {
self.export_rec(child_id, parser)?;
}
}
}
_ => (),
}
write!(self, r#"</div>"#)?;
}
write!(self, "\n </div>\n</div>")
}
}
impl fmt::Write for Html<'_> {
fn write_str(&mut self, s: &str) -> Result {
self.buf.write_str(s)
}
}
#[cfg(test)]
mod tests {
use super::*;
// use pretty_assertions::{assert_eq, assert_ne};
use pretty_assertions::assert_eq;
#[test]
fn combined_macros() -> fmt::Result {
let a = Html::export(
r"#+macro: poem hiii $1 $2 text
{{{poem(cool,three)}}}
",
)?;
assert_eq!(
a,
r"<p>
hiii cool three text
</p>
"
);
Ok(())
}
#[test]
fn keyword_macro() -> Result {
let a = Html::export(
r"
#+title: hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
{{{keyword(title)}}}
",
)?;
assert_eq!(
a,
r"<p>
hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
</p>
"
);
Ok(())
}
#[test]
fn line_break() -> Result {
let a = Html::export(
r" abc\\
",
)?;
assert_eq!(
a,
r"<p>
abc
<br>
</p>
"
);
let n = Html::export(
r" abc\\ q
",
)?;
assert_eq!(
n,
r"<p>
abc\\ q
</p>
"
);
Ok(())
}
#[test]
fn horizontal_rule() -> Result {
let a = Html::export(
r"-----
",
)?;
let b = Html::export(
r" -----
",
)?;
let c = Html::export(
r" -------------------------
",
)?;
assert_eq!(a, b);
assert_eq!(b, c);
assert_eq!(a, c);
let nb = Html::export(
r" ----
",
)?;
assert_eq!(
nb,
r"<p>
----
</p>
"
);
Ok(())
}
#[test]
fn correct_cache() -> Result {
let a = Html::export(
r"
- one
- two
\begin{align}
abc &+ 10\\
\end{align}
",
)?;
println!("{a}");
Ok(())
}
#[test]
fn html_unicode() -> Result {
let a = Html::export(
r"a é😳
",
)?;
assert_eq!(
a,
r"<p>
a é😳
</p>
"
);
Ok(())
}
#[test]
fn list_counter_set() -> Result {
let a = Html::export(
r"
1. [@4] wordsss??
",
)?;
assert_eq!(
a,
r#"<ol>
<li value="4"><p>
wordsss??
</p>
</li>
</ol>
"#
);
Ok(())
}
#[test]
fn anon_footnote() -> Result {
let a = Html::export(
r"
hi [fn:next:coolio]
",
)?;
// just codifying what the output is here, not supposed to be set in stone
assert_eq!(
a,
r##"<p>
hi <sup>
<a id="fnr.1" href="#fn.1" class="footref" role="doc-backlink">1</a>
</sup>
</p>
<div id="footnotes">
<style>
.footdef p {
display:inline;
}
</style>
<h2 class="footnotes">Footnotes</h2>
<div id="text-footnotes">
<div class="footdef">
<sup>
<a id="fn.1" href= "#fnr.1" role="doc-backlink">1</a>
</sup>
coolio</div>
</div>
</div>"##
);
Ok(())
}
#[test]
fn footnote_heading() -> Result {
let a = Html::export(
r"
hello [fn:1]
* Footnotes
[fn:1] world
",
)?;
// just codifying what the output is here, not supposed to be set in stone
assert_eq!(
a,
r##"<p>
hello <sup>
<a id="fnr.1" href="#fn.1" class="footref" role="doc-backlink">1</a>
</sup>
</p>
<h1 id="footnotes">Footnotes</h1>
<div id="footnotes">
<style>
.footdef p {
display:inline;
}
</style>
<div id="text-footnotes">
<div class="footdef">
<sup>
<a id="fn.1" href= "#fnr.1" role="doc-backlink">1</a>
</sup>
<p>
world
</p>
</div>
</div>
</div>"##
);
Ok(())
}
#[test]
fn footnote_order() -> Result {
// tests dupes too
let a = Html::export(
r#"
hi [fn:dupe] cool test [fn:coolnote] [fn:dupe:inlinefootnote]
coolest [fn:1] again [fn:1]
novel [fn:next:coolio]
** Footnotes
[fn:1] hi
[fn:dupe] abcdef
[fn:coolnote] words babby
"#,
)?;
// REVIEW; investigate different nodeids with export_buf and export
// had to change 1.7 to 1.8 to pass the test
assert_eq!(
a,
r##"<p>
hi <sup>
<a id="fnr.1" href="#fn.1" class="footref" role="doc-backlink">1</a>
</sup> cool test <sup>
<a id="fnr.2" href="#fn.2" class="footref" role="doc-backlink">2</a>
</sup> <sup>
<a id="fnr.1.8" href="#fn.1" class="footref" role="doc-backlink">1</a>
</sup> coolest <sup>
<a id="fnr.3" href="#fn.3" class="footref" role="doc-backlink">3</a>
</sup> again <sup>
<a id="fnr.3.13" href="#fn.3" class="footref" role="doc-backlink">3</a>
</sup>
</p>
<p>
novel <sup>
<a id="fnr.4" href="#fn.4" class="footref" role="doc-backlink">4</a>
</sup>
</p>
<h2 id="footnotes">Footnotes</h2>
<div id="footnotes">
<style>
.footdef p {
display:inline;
}
</style>
<div id="text-footnotes">
<div class="footdef">
<sup>
<a id="fn.1" href= "#fnr.1" role="doc-backlink">1</a>
</sup>
<p>
abcdef
</p>
</div>
<div class="footdef">
<sup>
<a id="fn.2" href= "#fnr.2" role="doc-backlink">2</a>
</sup>
<p>
words babby
</p>
</div>
<div class="footdef">
<sup>
<a id="fn.3" href= "#fnr.3" role="doc-backlink">3</a>
</sup>
<p>
hi
</p>
</div>
<div class="footdef">
<sup>
<a id="fn.4" href= "#fnr.4" role="doc-backlink">4</a>
</sup>
coolio</div>
</div>
</div>"##
);
Ok(())
}
#[test]
fn esoteric_footnotes() -> Result {
let a = Html::export(
r"
And anonymous ones [fn::mysterious]
what [fn::]
bad [fn:]
",
)?;
assert_eq!(
a,
r##"<p>
And anonymous ones <sup>
<a id="fnr.1" href="#fn.1" class="footref" role="doc-backlink">1</a>
</sup>
</p>
<p>
what <sup>
<a id="fnr.2" href="#fn.2" class="footref" role="doc-backlink">2</a>
</sup>
</p>
<p>
bad [fn:]
</p>
<div id="footnotes">
<style>
.footdef p {
display:inline;
}
</style>
<h2 class="footnotes">Footnotes</h2>
<div id="text-footnotes">
<div class="footdef">
<sup>
<a id="fn.1" href= "#fnr.1" role="doc-backlink">1</a>
</sup>
mysterious</div>
<div class="footdef">
<sup>
<a id="fn.2" href= "#fnr.2" role="doc-backlink">2</a>
</sup>
</div>
</div>
</div>"##
);
Ok(())
}
}