use aozora_core::node::{BlockParams, BlockType, MidashiLevel};
use super::tag_generator::{generate_block_end_tag, generate_block_start_tag};
#[derive(Debug, Clone)]
pub struct BlockContext {
pub block_type: BlockType,
pub params: BlockParams,
}
#[derive(Debug, Clone, Default)]
pub struct BlockManager {
stack: Vec<BlockContext>,
midashi_id_counter: u32,
}
impl BlockManager {
pub fn new() -> Self {
Self::default()
}
pub fn stack_len(&self) -> usize {
self.stack.len()
}
pub fn push(&mut self, block_type: BlockType, params: BlockParams) {
self.stack.push(BlockContext { block_type, params });
}
pub fn pop(&mut self) -> Option<BlockContext> {
self.stack.pop()
}
#[allow(dead_code)]
pub fn remove(&mut self, pos: usize) -> BlockContext {
self.stack.remove(pos)
}
#[allow(dead_code)]
pub fn iter(&self) -> impl Iterator<Item = &BlockContext> {
self.stack.iter()
}
#[allow(dead_code)]
pub fn has_blocks(&self) -> bool {
!self.stack.is_empty()
}
pub fn pop_to_length(&mut self, target_len: usize) -> Vec<(BlockType, BlockParams)> {
let mut result = Vec::new();
while self.stack.len() > target_len {
if let Some(ctx) = self.stack.pop() {
result.push((ctx.block_type, ctx.params));
}
}
result
}
pub fn find_burasage_context(&self) -> Option<(u32, i32)> {
for ctx in &self.stack {
if ctx.block_type == BlockType::Burasage {
let wrap_width = ctx.params.wrap_width.unwrap_or(1);
let width = ctx.params.width.unwrap_or(0);
let text_indent = width as i32 - wrap_width as i32;
return Some((wrap_width, text_indent));
}
}
None
}
pub fn close_inline_blocks(&mut self) -> Vec<(BlockType, BlockParams)> {
let mut result = Vec::new();
while let Some(pos) = self.stack.iter().rposition(|c| !c.params.is_block) {
let ctx = self.stack.remove(pos);
result.push((ctx.block_type, ctx.params));
}
result
}
pub fn close_related_blocks(
&mut self,
new_block_type: &BlockType,
) -> Vec<(BlockType, BlockParams)> {
let mut result = Vec::new();
if *new_block_type == BlockType::Jisage
|| *new_block_type == BlockType::Chitsuki
|| *new_block_type == BlockType::Burasage
{
while let Some(pos) = self.stack.iter().rposition(|c| {
c.block_type == *new_block_type
|| c.block_type == BlockType::Burasage
|| (*new_block_type == BlockType::Jisage && c.block_type == BlockType::Jisage)
|| (*new_block_type == BlockType::Burasage && c.block_type == BlockType::Jisage)
}) {
let ctx = self.stack.remove(pos);
if ctx.block_type != BlockType::Burasage {
result.push((ctx.block_type, ctx.params));
}
}
}
result
}
pub fn find_and_close(&mut self, block_type: &BlockType) -> Option<BlockContext> {
let pos = self.stack.iter().rposition(|c| {
c.block_type == *block_type
|| (*block_type == BlockType::Jisage && c.block_type == BlockType::Burasage)
});
pos.map(|p| self.stack.remove(p))
}
pub fn generate_midashi_id(&mut self, level: MidashiLevel) -> u32 {
let increment = match level {
MidashiLevel::O => 100,
MidashiLevel::Naka => 10,
MidashiLevel::Ko => 1,
};
self.midashi_id_counter += increment;
self.midashi_id_counter
}
pub fn render_block_start_tag(
&mut self,
block_type: &BlockType,
params: &BlockParams,
) -> String {
let midashi_id = if *block_type == BlockType::Midashi {
let level = params.level.unwrap_or(MidashiLevel::O);
Some(self.generate_midashi_id(level))
} else {
None
};
generate_block_start_tag(block_type, params, midashi_id)
}
pub fn render_block_end_tag(&self, block_type: &BlockType, params: &BlockParams) -> String {
generate_block_end_tag(block_type, params)
}
}