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
pub trait FormatAttribute { fn format_attribute(&self) -> String; } impl<T> FormatAttribute for Vec<T> where T: FormatAttribute, { fn format_attribute(&self) -> String { let mut out = String::new(); let mut it = self.iter().peekable(); while let Some(next) = it.next() { out.push_str(&next.format_attribute()); if !it.peek().is_none() { out.push_str(" "); } } out } } impl<'a> FormatAttribute for &'a str { fn format_attribute(&self) -> String { (*self).to_owned() } } impl FormatAttribute for String { fn format_attribute(&self) -> String { self.clone() } } #[macro_export] macro_rules! define_processor { ($name:ident, $body:ty, $slf:ident, $($tail:tt)*) => ( pub struct $name<'env> { pub out: ::std::cell::RefCell<DocBuilder<'env>>, pub env: &'env Environment, pub syntax: (&'env ::syntect::highlighting::Theme, &'env ::syntect::parsing::SyntaxSet), pub root: &'env str, pub body: &'env $body, } impl<'env> Processor<'env> for $name<'env> { fn env(&self) -> &'env Environment { self.env } fn out(&self) -> ::std::cell::RefMut<DocBuilder<'env>> { self.out.borrow_mut() } fn root(&self) -> &'env str { self.root } fn syntax(&self) -> ( &'env ::syntect::highlighting::Theme, &'env ::syntect::parsing::SyntaxSet, ) { self.syntax } define_processor!(@tail $slf $($tail)*); } ); (@tail $slf:ident process => $body:block; $($tail:tt)*) => ( fn process($slf) -> Result<()> $body define_processor!(@tail $slf $($tail)*); ); (@tail $slf:ident current_package => $body:block; $($tail:tt)*) => ( fn current_package(&$slf) -> Option<&'env ::core::RpVersionedPackage> $body define_processor!(@tail $slf $($tail)*); ); (@tail $slf:ident current_package => $expr:expr; $($tail:tt)*) => ( fn current_package(&$slf) -> Option<&'env ::core::RpVersionedPackage> { Some($expr) } define_processor!(@tail $slf $($tail)*); ); (@tail $slf:ident) => (); } #[macro_export] macro_rules! html { (@open $slf:ident, $element:ident {$($key:ident => $value:expr),*}) => {{ write!($slf.out(), "<{}", stringify!($element))?; $( write!($slf.out(), " {}=\"", stringify!($key))?; $slf.out().write_str(&$value.format_attribute())?; write!($slf.out(), "\"")?; )* write!($slf.out(), ">")?; }}; (@close $slf:ident, $element:ident) => {{ write!($slf.out(), "</{}>", stringify!($element))?; }}; ($slf:ident, $element:ident {$($key:ident => $value:expr),*} => $body:block) => {{ html!(@open $slf, $element {$($key=> $value),*}); $slf.out().new_line()?; $slf.out().indent(); $body; $slf.out().new_line_unless_empty()?; $slf.out().unindent(); html!(@close $slf, $element); $slf.out().new_line()?; }}; ($slf:ident, $element:ident {$($key:ident => $value:expr),*} ~ $body:expr) => {{ html!(@open $slf, $element {$($key=> $value),*}); write!($slf.out(), "{}", $body)?; html!(@close $slf, $element); $slf.out().new_line()?; }}; ($slf:ident, $element:ident {$($key:ident => $value:expr),*}) => { html!($element {$($key=> $value),*}, $slf => {}) }; ($slf:ident, $element:ident $body:expr) => { html!($element {} $body) }; }