DEV Community

Cover image for Adding New Features to Rssg and Git Merging Branches
Antonio-Bennett
Antonio-Bennett

Posted on • Edited on

Adding New Features to Rssg and Git Merging Branches

If you're read my blog you know that I have been slowly making a static site generator for my OSD600 class. This week I added 2 more features;

  • Horizontal Rule
  • Inline code parsing

Horizontal Rule

To enable this feature was quite easy. In my process function, where I parse the lines of the markdown file, I look at each line and take the necessary steps. I check if the current line only contains --- and if so I add an <hr> tag. The prev_tag variable is needed to correctly write paragraphs that are not separated by hard new lines

if curr_line.trim() == "---" {
    line = "\t<hr>".to_owned();
    prev_tag = "<hr>";
} else {
    line = "\t<p>".to_owned() + curr_line;
    prev_tag = "<p>";
}
Enter fullscreen mode Exit fullscreen mode

Inline code

The inline code was much harder to come up with a solution for. I knew that I would have to check the built line write before it is written to the file for backticks however rust does not allow you to overwrite a character type with a string if you traverse the line by characters;

line.chars().for_each(|mut c| {
    if *c == '`' { c = "<code>"; }
}
Enter fullscreen mode Exit fullscreen mode

This would not work because a &str or string cannot be assigned to a value of type &[u8] which is due to chars() iterating a string by an array of u8 bytes. What I had to do was count the number of backticks and if even we know we can replace all of them otherwise all but the last odd backtick. The we can have a boolean to switch between an open and closed tag state. Then we take a substring from the beginning of the string to the backticks and add them together with the correct tags.

if line.contains('`') {
//get num of backticks to know if we should ignore the last one
    let num = line.chars().filter(|c| *c == '`').count();
    let mut open = true; //to switch between open and close tag of code

    //even amount so we can replace freely
        if num % 2 == 0 {
            for _ in 0..num {
                    let x = line.find('`').unwrap();
                        if open {
                            line = line[0..x].to_owned() + "<code>" + &line[x + 1..];
                                open = false;
                        } else {
                                line = line[0..x].to_owned() + "</code>" + &line[x + 1..];
                                open = true;
                        }
                  }
         } else {
            //Replace all but the last odd backtick
                for _ in 1..num {
                    let x = line.find('`').unwrap();
                        if open {
                            line = line[0..x].to_owned() + "<code>" + &line[x + 1..];
                                open = false;
                        } else {
                                line = line[0..x].to_owned() + "</code>" + &line[x + 1..];
                                open = true;
                        }
                  }
          }
}
html.write_all(line.as_bytes())
    .expect("Could not write to file");

Enter fullscreen mode Exit fullscreen mode

Merging

These 2 changes were done on two different branches. The merge process was actually quite simple and although there were merge conflicts with the second merge it was easy to resolve by simply choosing the correct code that should stay in the merge process.

You can find the commits here: Horizontal Rule, Inline Code

Repo: Rssg

Top comments (0)