| 1 | use derivative::Derivative; |
| 2 | |
| 3 | use crate::common::ruler::Ruler; |
| 4 | use crate::common::sourcemap::SourcePos; |
| 5 | use crate::common::TypeKey; |
| 6 | use crate::parser::block::{self, BlockParser}; |
| 7 | use crate::parser::core::{Root, *}; |
| 8 | use crate::parser::extset::MarkdownItExtSet; |
| 9 | use crate::parser::inline::{self, InlineParser}; |
| 10 | use crate::parser::linkfmt::{LinkFormatter, MDLinkFormatter}; |
| 11 | use crate::Node; |
| 12 | |
| 13 | type RuleFn = fn (&mut Node, &MarkdownIt); |
| 14 | |
| 15 | #[derive(Derivative)] |
| 16 | #[derivative(Debug)] |
| 17 | /// Main parser struct, created once and reused for parsing multiple documents. |
| 18 | pub struct MarkdownIt { |
| 19 | /// Block-level tokenizer. |
| 20 | pub block: BlockParser, |
| 21 | |
| 22 | /// Inline-level tokenizer. |
| 23 | pub inline: InlineParser, |
| 24 | |
| 25 | /// Link validator and formatter. |
| 26 | pub link_formatter: Box<dyn LinkFormatter>, |
| 27 | |
| 28 | /// Storage for custom data used in plugins. |
| 29 | pub ext: MarkdownItExtSet, |
| 30 | |
| 31 | /// Maximum depth of the generated AST, exists to prevent recursion |
| 32 | /// (if markdown source reaches this depth, deeply nested structures |
| 33 | /// will be parsed as plain text). |
| 34 | /// TODO: doesn't work |
| 35 | #[doc(hidden)] |
| 36 | pub max_nesting: u32, |
| 37 | |
| 38 | /// Maximum allowed indentation for syntax blocks |
| 39 | /// default i32::MAX, indented code blocks will set this to 4 |
| 40 | pub max_indent: i32, |
| 41 | |
| 42 | ruler: Ruler<TypeKey, RuleFn>, |
| 43 | } |
| 44 | |
| 45 | impl MarkdownIt { |
| 46 | pub fn new() -> Self { |
| 47 | Self::default() |
| 48 | } |
| 49 | |
| 50 | pub fn parse(&self, src: &str) -> Node { |
| 51 | let mut node = Node::new(Root::new(src.to_owned())); |
| 52 | node.srcmap = Some(SourcePos::new(0, src.len())); |
| 53 | |
| 54 | for rule in self.ruler.iter() { |
| 55 | rule(&mut node, self); |
| 56 | debug_assert!(node.is::<Root>(), "root node of the AST must always be Root"); |
| 57 | } |
| 58 | node |
| 59 | } |
| 60 | |
| 61 | pub fn add_rule<T: CoreRule>(&mut self) -> RuleBuilder<RuleFn> { |
| 62 | let item = self.ruler.add(TypeKey::of::<T>(), T::run); |
| 63 | RuleBuilder::new(item) |
| 64 | } |
| 65 | |
| 66 | pub fn has_rule<T: CoreRule>(&mut self) -> bool { |
| 67 | self.ruler.contains(TypeKey::of::<T>()) |
| 68 | } |
| 69 | |
| 70 | pub fn remove_rule<T: CoreRule>(&mut self) { |
| 71 | self.ruler.remove(TypeKey::of::<T>()); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | impl Default for MarkdownIt { |
| 76 | fn default() -> Self { |
| 77 | let mut md = Self { |
| 78 | block: BlockParser::new(), |
| 79 | inline: InlineParser::new(), |
| 80 | link_formatter: Box::new(MDLinkFormatter::new()), |
| 81 | ext: MarkdownItExtSet::new(), |
| 82 | max_nesting: 100, |
| 83 | ruler: Ruler::new(), |
| 84 | max_indent: i32::MAX, |
| 85 | }; |
| 86 | block::builtin::add(&mut md); |
| 87 | inline::builtin::add(&mut md); |
| 88 | md |
| 89 | } |
| 90 | } |