Adding New Features to Rssg and Git Merging Branches
Tags: rust, opensource, github, osd600
Published: 2021-10-01 00:49:18

If you're read my blog you know that I have been slowly making a [static site generator](https://github.com/Antonio-Bennett/rssg) 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 

```rust
if curr_line.trim() == "---" {
    line = "\t<hr>".to_owned();
    prev_tag = "<hr>";
} else {
    line = "\t<p>".to_owned() + curr_line;
    prev_tag = "<p>";
}
```

### 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;

```rust
line.chars().for_each(|mut c| {
    if *c == '`' { c = "<code>"; }
}
```
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.

```rust
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");

```

### 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](https://github.com/Antonio-Bennett/rssg/commit/8b70bf0f), [Inline Code](https://github.com/Antonio-Bennett/rssg/commit/b52938a7)

Repo: [Rssg](https://github.com/Antonio-Bennett/rssg)