fn concat_or_push_anchor(
    text_node: NodeDataRef<RefCell<String>>,
    text_nodes: &mut impl Iterator<Item=NodeDataRef<RefCell<String>>>,
    blocks: &mut Vec<Block>,
    ) -> Result<(), Error>
{
    let a_node = text_node.as_node()
        .parent()
        .expect("Anchor node parent must already exist");

    // First check if parent of anchor node is text block
    // If not, then return an error that anchor is not
    // in a text block, and therefore cannot be pushed
    // TODO understand with_context better
    // TODO write tests for case where anchor node is nested
    // inside non-text block with text

    let text = text_node.as_node().to_string();
    let mut anchor_block = Block::new(BlockTag::A, text);

    // Then check if there's text to concatenate to on either side
    // first concatenate next text into anchor, then
    // anchor into previous. This will use the fewest allocations
    // and movement?
    // (algo designed for doing following node first, if changing
    // make sure to take order into consideration.

    if let Some(following_node) = a_node.following_siblings().next() {
        if following_node.as_text().is_some() {
            if let Some(following_node) = text_nodes.next() {
                let text = following_node.as_node().to_string();
                anchor_block.text.push_str(&text);
                anchor_block.word_count += count_words(&text);
                // the following node anchor word count must be 0
            }
        }
    }

    if let Some(previous_node) = a_node.preceding_siblings().next() {
        if previous_node.as_text().is_some() {
            if let Some(previous_block) = blocks.last_mut() {
                previous_block.text.push_str(&anchor_block.text);
                previous_block.word_count += anchor_block.word_count;
                previous_block.anchor_word_count += anchor_block.anchor_word_count;
            }
        }
    } else {
        blocks.push(anchor_block);
    }

    Ok(())
}
/// If an anchor text is surrounded by text nodes, then
/// concatenate with the surrounding text
/// but if following is not text, just append anchor.
If an anchor text is surrounded by text nodes, then
concatenate with the surrounding text
but if following is not text, just append anchor.
If an anchor text is surrounded by text nodes then concatenate with the surrounding text but if following is not text just append anchor .
fn concat_or_push_anchor(
    text_node: NodeDataRef<RefCell<String>>,
    text_nodes: &mut impl Iterator<Item=NodeDataRef<RefCell<String>>>,
    blocks: &mut Vec<Block>,
    ) -> Result<(), Error>
{
    let a_node = text_node.as_node()
        .parent()
        .expect("Anchor node parent must already exist");
    // First check if parent of anchor node is text block
    // If not, then return an error that anchor is not
    // in a text block, and therefore cannot be pushed
    // TODO understand with_context better
    // TODO write tests for case where anchor node is nested
    // inside non-text block with text
    let text = text_node.as_node().to_string();
    let mut anchor_block = Block::new(BlockTag::A, text);
    // Then check if there's text to concatenate to on either side
    // first concatenate next text into anchor, then
    // anchor into previous. This will use the fewest allocations
    // and movement?
    // (algo designed for doing following node first, if changing
    // make sure to take order into consideration.
    if let Some(following_node) = a_node.following_siblings().next() {
        if following_node.as_text().is_some() {
            if let Some(following_node) = text_nodes.next() {
                let text = following_node.as_node().to_string();
                anchor_block.text.push_str(&text);
                anchor_block.word_count += count_words(&text);
                // the following node anchor word count must be 0
            }
        }
    }
    if let Some(previous_node) = a_node.preceding_siblings().next() {
        if previous_node.as_text().is_some() {
            if let Some(previous_block) = blocks.last_mut() {
                previous_block.text.push_str(&anchor_block.text);
                previous_block.word_count += anchor_block.word_count;
                previous_block.anchor_word_count += anchor_block.anchor_word_count;
            }
        }
    } else {
        blocks.push(anchor_block);
    }
    Ok(())
}
fn concat_or_push_anchor ( text_node : NodeDataRef < RefCell < String > > , text_nodes : & mut impl Iterator < Item = NodeDataRef < RefCell < String > > > , blocks : & mut Vec < Block > , ) -> Result < ( ) , Error > { let a_node = text_node . as_node ( ) . parent ( ) . expect ( " " ) ; // First check if parent of anchor node is text block // If not, then return an error that anchor is not // in a text block, and therefore cannot be pushed // TODO understand with_context better // TODO write tests for case where anchor node is nested // inside non-text block with text let text = text_node . as_node ( ) . to_string ( ) ; let mut anchor_block = Block :: new ( BlockTag :: A , text ) ; // Then check if there's text to concatenate to on either side // first concatenate next text into anchor, then // anchor into previous. This will use the fewest allocations // and movement? // (algo designed for doing following node first, if changing // make sure to take order into consideration. if let Some ( following_node ) = a_node . following_siblings ( ) . next ( ) { if following_node . as_text ( ) . is_some ( ) { if let Some ( following_node ) = text_nodes . next ( ) { let text = following_node . as_node ( ) . to_string ( ) ; anchor_block . text . push_str ( & text ) ; anchor_block . word_count += count_words ( & text ) ; // the following node anchor word count must be 0 } } } if let Some ( previous_node ) = a_node . preceding_siblings ( ) . next ( ) { if previous_node . as_text ( ) . is_some ( ) { if let Some ( previous_block ) = blocks . last_mut ( ) { previous_block . text . push_str ( & anchor_block . text ) ; previous_block . word_count += anchor_block . word_count ; previous_block . anchor_word_count += anchor_block . anchor_word_count ; } } } else { blocks . push ( anchor_block ) ; } Ok ( ( ) ) }
If an anchor text is surrounded by text nodes, then
concatenate with the surrounding text
but if following is not text, just append anchor.
If an anchor text is surrounded by text nodes then concatenate with the surrounding text but if following is not text just append anchor .
text_node
NodeDataRef<RefCell<String>>
text_nodes
&mut impl Iterator<Item=NodeDataRef<RefCell<String>>>
blocks
&mut Vec<Block>
text_node
NodeDataRef<RefCell<String>>
text_nodes
&mut impl Iterator<Item=NodeDataRef<RefCell<String>>>
blocks
&mut Vec<Block>