- pub fn parse(source: &str) -> String {
-
- let lines = source.split("\n");
- let mut is_preformatted = false;
-
- let mut block_label: Option<String> = None;
- let mut html: String = "".to_owned();
- let mut current_line_type: Option<LineType> = None;
-
- let mut heading_stack: Vec<u8> = Vec::new();
- for line in lines {
- let mut line_type = LineType::Blank;
- if line.char_indices().count() > 2 {
- let mut end = line.len();
- if line.char_indices().count() > 3 {
- end = line.char_indices().map(|(i, _)| i).nth(3).unwrap();
- }
- line_type = identify_line(&line[..end], is_preformatted);
- }
- match line_type {
- LineType::PreformattedToggle => {
- is_preformatted = !is_preformatted;
- if is_preformatted && line.char_indices().count() > 3 {
- block_label = Some(get_partial_line_content(&line_type, line));
- } else {
- block_label = None;
- }
- },
- _ => {
- // Close previous block if needed
- if let Some(line) = ¤t_line_type {
- if line != &line_type && is_block(line) {
- html.push_str(get_line_closer(line));
- }
- }
-
- // Blocks
- if is_block(&line_type) {
- if let Some(line) = ¤t_line_type {
- if line != &line_type {
- html.push_str(&get_line_opener(&line_type, block_label.as_ref()));
- }
- } else {
- html.push_str(&get_line_opener(&line_type, None));
- }
-
- let line_content = get_partial_line_content(&line_type, line);
- html.push_str(&line_content);
- } else {
- html.push_str(&get_heading_wrapper(&mut heading_stack, &line_type));
- html.push_str(&get_full_line_content(&line_type, line));
- }
- current_line_type = Some(line_type);
- },
- }
- }
- if let Some(line) = ¤t_line_type {
- if is_block(line) {
- html.push_str(get_line_closer(line));
- }
- }
- html.push_str(&close_heading_wrapper(&mut heading_stack));
- html