block: move Block::from to Kind::block
allow more inputs
This commit is contained in:
		
					parent
					
						
							
								6c630798f8
							
						
					
				
			
			
				commit
				
					
						9ab8be5d30
					
				
			
		
					 1 changed files with 32 additions and 40 deletions
				
			
		
							
								
								
									
										72
									
								
								src/block.rs
									
										
									
									
									
								
							
							
						
						
									
										72
									
								
								src/block.rs
									
										
									
									
									
								
							| 
						 | 
					@ -241,9 +241,9 @@ impl<'s> TreeParser<'s> {
 | 
				
			||||||
                self.prev_blankline = false;
 | 
					                self.prev_blankline = false;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            match Block::from(&kind) {
 | 
					            match kind.block(top_level) {
 | 
				
			||||||
                Block::Atom(a) => self.tree.atom(a, span),
 | 
					                Block::Atom(a) => self.tree.atom(a, span),
 | 
				
			||||||
                Block::Leaf(l) => self.parse_leaf(l, &kind, span, lines, top_level),
 | 
					                Block::Leaf(l) => self.parse_leaf(l, &kind, span, lines),
 | 
				
			||||||
                Block::Container(Table) => self.parse_table(lines, span),
 | 
					                Block::Container(Table) => self.parse_table(lines, span),
 | 
				
			||||||
                Block::Container(c) => self.parse_container(c, &kind, span, lines),
 | 
					                Block::Container(c) => self.parse_container(c, &kind, span, lines),
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
| 
						 | 
					@ -254,14 +254,7 @@ impl<'s> TreeParser<'s> {
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn parse_leaf(
 | 
					    fn parse_leaf(&mut self, leaf: Leaf, k: &Kind, span: Span, lines: &mut [Span]) {
 | 
				
			||||||
        &mut self,
 | 
					 | 
				
			||||||
        mut leaf: Leaf,
 | 
					 | 
				
			||||||
        k: &Kind,
 | 
					 | 
				
			||||||
        span: Span,
 | 
					 | 
				
			||||||
        lines: &mut [Span],
 | 
					 | 
				
			||||||
        top_level: bool,
 | 
					 | 
				
			||||||
    ) {
 | 
					 | 
				
			||||||
        if let Kind::Fenced { indent, .. } = k {
 | 
					        if let Kind::Fenced { indent, .. } = k {
 | 
				
			||||||
            for line in lines.iter_mut() {
 | 
					            for line in lines.iter_mut() {
 | 
				
			||||||
                let indent_line = line.len() - line.trim_start(self.src).len();
 | 
					                let indent_line = line.len() - line.trim_start(self.src).len();
 | 
				
			||||||
| 
						 | 
					@ -283,7 +276,10 @@ impl<'s> TreeParser<'s> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if let Kind::Heading { level, .. } = k {
 | 
					        if let Kind::Heading { level, .. } = k {
 | 
				
			||||||
            // open and close sections
 | 
					            // open and close sections
 | 
				
			||||||
            if top_level {
 | 
					            if let Leaf::Heading {
 | 
				
			||||||
 | 
					                has_section: true, ..
 | 
				
			||||||
 | 
					            } = leaf
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
                let first_close = self
 | 
					                let first_close = self
 | 
				
			||||||
                    .open_sections
 | 
					                    .open_sections
 | 
				
			||||||
                    .iter()
 | 
					                    .iter()
 | 
				
			||||||
| 
						 | 
					@ -302,10 +298,6 @@ impl<'s> TreeParser<'s> {
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if let Leaf::Heading { has_section } = &mut leaf {
 | 
					 | 
				
			||||||
            *has_section = top_level;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        self.tree.enter(Node::Leaf(leaf), span);
 | 
					        self.tree.enter(Node::Leaf(leaf), span);
 | 
				
			||||||
        lines
 | 
					        lines
 | 
				
			||||||
            .iter()
 | 
					            .iter()
 | 
				
			||||||
| 
						 | 
					@ -574,31 +566,6 @@ struct IdentifiedBlock {
 | 
				
			||||||
    span: Span,
 | 
					    span: Span,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl From<&Kind> for Block {
 | 
					 | 
				
			||||||
    fn from(kind: &Kind) -> Self {
 | 
					 | 
				
			||||||
        match kind {
 | 
					 | 
				
			||||||
            Kind::Atom(a) => Self::Atom(*a),
 | 
					 | 
				
			||||||
            Kind::Paragraph => Self::Leaf(Paragraph),
 | 
					 | 
				
			||||||
            Kind::Heading { .. } => Self::Leaf(Heading { has_section: false }),
 | 
					 | 
				
			||||||
            Kind::Fenced {
 | 
					 | 
				
			||||||
                kind: FenceKind::CodeBlock(..),
 | 
					 | 
				
			||||||
                ..
 | 
					 | 
				
			||||||
            } => Self::Leaf(CodeBlock),
 | 
					 | 
				
			||||||
            Kind::Fenced {
 | 
					 | 
				
			||||||
                kind: FenceKind::Div,
 | 
					 | 
				
			||||||
                ..
 | 
					 | 
				
			||||||
            } => Self::Container(Div),
 | 
					 | 
				
			||||||
            Kind::Definition {
 | 
					 | 
				
			||||||
                footnote: false, ..
 | 
					 | 
				
			||||||
            } => Self::Leaf(LinkDefinition),
 | 
					 | 
				
			||||||
            Kind::Definition { footnote: true, .. } => Self::Container(Footnote),
 | 
					 | 
				
			||||||
            Kind::Blockquote => Self::Container(Blockquote),
 | 
					 | 
				
			||||||
            Kind::ListItem { ty, .. } => Self::Container(ListItem(*ty)),
 | 
					 | 
				
			||||||
            Kind::Table { .. } => Self::Container(Table),
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
impl IdentifiedBlock {
 | 
					impl IdentifiedBlock {
 | 
				
			||||||
    fn new(line: &str) -> Self {
 | 
					    fn new(line: &str) -> Self {
 | 
				
			||||||
        let indent = line
 | 
					        let indent = line
 | 
				
			||||||
| 
						 | 
					@ -863,6 +830,31 @@ impl Kind {
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn block(&self, top_level: bool) -> Block {
 | 
				
			||||||
 | 
					        match self {
 | 
				
			||||||
 | 
					            Self::Atom(a) => Block::Atom(*a),
 | 
				
			||||||
 | 
					            Self::Paragraph => Block::Leaf(Paragraph),
 | 
				
			||||||
 | 
					            Self::Heading { .. } => Block::Leaf(Heading {
 | 
				
			||||||
 | 
					                has_section: top_level,
 | 
				
			||||||
 | 
					            }),
 | 
				
			||||||
 | 
					            Self::Fenced {
 | 
				
			||||||
 | 
					                kind: FenceKind::CodeBlock(..),
 | 
				
			||||||
 | 
					                ..
 | 
				
			||||||
 | 
					            } => Block::Leaf(CodeBlock),
 | 
				
			||||||
 | 
					            Self::Fenced {
 | 
				
			||||||
 | 
					                kind: FenceKind::Div,
 | 
				
			||||||
 | 
					                ..
 | 
				
			||||||
 | 
					            } => Block::Container(Div),
 | 
				
			||||||
 | 
					            Self::Definition {
 | 
				
			||||||
 | 
					                footnote: false, ..
 | 
				
			||||||
 | 
					            } => Block::Leaf(LinkDefinition),
 | 
				
			||||||
 | 
					            Self::Definition { footnote: true, .. } => Block::Container(Footnote),
 | 
				
			||||||
 | 
					            Self::Blockquote => Block::Container(Blockquote),
 | 
				
			||||||
 | 
					            Self::ListItem { ty, .. } => Block::Container(ListItem(*ty)),
 | 
				
			||||||
 | 
					            Self::Table { .. } => Block::Container(Table),
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl std::fmt::Display for Block {
 | 
					impl std::fmt::Display for Block {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue