Skip to content

Commit f3601dd

Browse files
committed
engine_spx2html: start using the new html::Element type
1 parent 452f2e7 commit f3601dd

2 files changed

Lines changed: 38 additions & 30 deletions

File tree

crates/engine_spx2html/src/html.rs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,79 @@
11
// Copyright 2021-2022 the Tectonic Project
22
// Licensed under the MIT License.
33

4-
//! Helpers for working with HTML5 tags.
4+
//! Helpers for working with HTML5 elements.
55
66
use std::str::FromStr;
77

8-
macro_rules! emit_tag_data {
8+
macro_rules! emit_element_data {
99
($([
1010
$vname:ident
1111
$tagname:literal
1212
deprecated($deprecated:literal)
1313
empty($empty:literal)
1414
autoclosed($($acvname:ident),*)
1515
],)+) => {
16-
pub enum Tag {
16+
#[derive(Clone, Debug, Eq, PartialEq)]
17+
pub enum Element {
1718
$($vname,)+
1819
Other(String),
1920
}
2021

21-
impl FromStr for Tag {
22+
impl FromStr for Element {
2223
type Err = ();
2324

2425
fn from_str(s: &str) -> Result<Self, Self::Err> {
2526
Ok(match s {
26-
$($tagname => Tag::$vname,)+
27-
other => Tag::Other(other.to_owned())
27+
$($tagname => Element::$vname,)+
28+
other => Element::Other(other.to_owned())
2829
})
2930
}
3031
}
3132

32-
impl Tag {
33+
impl Element {
34+
pub fn name(&self) -> &str {
35+
match self {
36+
$(Element::$vname => $tagname,)+
37+
Element::Other(ref name) => name,
38+
}
39+
}
40+
3341
pub fn is_deprecated(&self) -> bool {
3442
match self {
35-
$(Tag::$vname => $deprecated,)+
36-
Tag::Other(_) => false,
43+
$(Element::$vname => $deprecated,)+
44+
Element::Other(_) => false,
3745
}
3846
}
3947

4048
pub fn is_empty(&self) -> bool {
4149
match self {
42-
$(Tag::$vname => $empty,)+
43-
Tag::Other(_) => false,
50+
$(Element::$vname => $empty,)+
51+
Element::Other(_) => false,
4452
}
4553
}
4654

4755
pub fn is_other(&self) -> bool {
48-
matches!(self, Tag::Other(_))
56+
matches!(self, Element::Other(_))
4957
}
5058

51-
pub fn is_autoclosed_by(&self, other: &Tag) -> bool {
59+
pub fn is_autoclosed_by(&self, other: &Element) -> bool {
5260
match self {
5361
$(
54-
Tag::$vname => {
62+
Element::$vname => {
5563
match other {
56-
$(Tag::$acvname => true,)*
64+
$(Element::$acvname => true,)*
5765
_ => false
5866
}
5967
},
6068
)+
61-
Tag::Other(_) => false,
69+
Element::Other(_) => false,
6270
}
6371
}
6472
}
6573
}
6674
}
6775

68-
emit_tag_data! {
76+
emit_element_data! {
6977
[A "a" deprecated(false) empty(false) autoclosed()],
7078
[Abbr "abbr" deprecated(false) empty(false) autoclosed()],
7179
[Acronym "acroynm" deprecated(true) empty(false) autoclosed()],

crates/engine_spx2html/src/lib.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -603,10 +603,10 @@ struct EmittingState {
603603

604604
#[derive(Debug)]
605605
struct ElementState {
606-
/// The tag name of the associated HTML element. This is None for the bottom
607-
/// item in the stack, or for changes in state that are not associated with
608-
/// actual HTML tags.
609-
elem: Option<String>,
606+
/// The associated HTML element. This is None for the bottom item in the
607+
/// stack, or for changes in state that are not associated with actual HTML
608+
/// tags.
609+
elem: Option<html::Element>,
610610

611611
/// The origin of this element/state-change.
612612
origin: ElementOrigin,
@@ -707,7 +707,7 @@ impl EmittingState {
707707
if let Some(e) = cur.elem.as_ref() {
708708
self.current_content.push('<');
709709
self.current_content.push('/');
710-
self.current_content.push_str(e);
710+
self.current_content.push_str(e.name());
711711
self.current_content.push('>');
712712
}
713713
}
@@ -728,11 +728,11 @@ impl EmittingState {
728728
}
729729
}
730730

731-
fn push_elem<D: std::fmt::Display>(&mut self, name: D, origin: ElementOrigin) {
731+
fn push_elem(&mut self, name: &str, origin: ElementOrigin) {
732732
self.close_automatics();
733733

734734
let new_item = ElementState {
735-
elem: Some(name.to_string()),
735+
elem: Some(name.parse().unwrap()),
736736
origin,
737737
..*self.cur_elstate()
738738
};
@@ -752,11 +752,11 @@ impl EmittingState {
752752
if let Some(e) = cur.elem.as_ref() {
753753
self.current_content.push('<');
754754
self.current_content.push('/');
755-
self.current_content.push_str(e);
755+
self.current_content.push_str(e.name());
756756
self.current_content.push('>');
757757
n_closed += 1;
758758

759-
if e == name {
759+
if e.name() == name {
760760
break;
761761
}
762762
}
@@ -994,7 +994,7 @@ impl EmittingState {
994994
}
995995

996996
let mut elstate = ElementState {
997-
elem: Some(tagname.to_owned()),
997+
elem: Some(tagname.parse().unwrap()),
998998
origin: ElementOrigin::Manual,
999999
..*self.cur_elstate()
10001000
};
@@ -1398,7 +1398,7 @@ impl EmittingState {
13981398
self.push_space_if_needed(x0, Some(fnum));
13991399
self.current_content.push_str("<b>");
14001400
self.elem_stack.push(ElementState {
1401-
elem: Some("b".to_owned()),
1401+
elem: Some(html::Element::B),
14021402
origin: ElementOrigin::FontAuto,
14031403
active_font: af,
14041404
..*self.cur_elstate()
@@ -1409,7 +1409,7 @@ impl EmittingState {
14091409
self.push_space_if_needed(x0, Some(fnum));
14101410
self.current_content.push_str("<i>");
14111411
self.elem_stack.push(ElementState {
1412-
elem: Some("i".to_owned()),
1412+
elem: Some(html::Element::I),
14131413
origin: ElementOrigin::FontAuto,
14141414
active_font: af,
14151415
..*self.cur_elstate()
@@ -1430,7 +1430,7 @@ impl EmittingState {
14301430
.unwrap();
14311431

14321432
self.elem_stack.push(ElementState {
1433-
elem: Some("span".to_owned()),
1433+
elem: Some(html::Element::Span),
14341434
origin: ElementOrigin::FontAuto,
14351435
active_font: desired_af,
14361436
..*self.cur_elstate()

0 commit comments

Comments
 (0)