Browse Source

Added more documentation to Buffer.cpp, stopped assuming trailing spaces after tags and updated the test file.

cameron
Cameron Weinfurt 3 years ago
parent
commit
e925728c3d
  1. 14
      src/Buffer.cpp
  2. 10
      test.txt

14
src/Buffer.cpp

@ -40,7 +40,7 @@ bool Buffer::open(const string & new_file_name)
std::string curr_p, line;
while(getline(file, line))
{
// Replace break tags with newlines.
// Replace break tags with newlines. This is like HTML in that the tags are directly replaced with newline characters.
for(auto char_loc = line.find("<br>"); char_loc != -1; char_loc = line.find("<br>"))
line.replace(char_loc, 4, "\n");
@ -49,36 +49,42 @@ bool Buffer::open(const string & new_file_name)
{
++curr_link;
// Find the length of the tag and pull out the data from the tag.
auto tag_len = line.find_first_of('>', tag_loc) - tag_loc;
std::string link_tag = line.substr(tag_loc + 3, tag_len - 3);
// Seperate the link path and link name into seperate strings. Assuming no spaces in the link path.
auto second_space_loc = link_tag.find_first_of(' ', 0);
std::string file_name = link_tag.substr(0, second_space_loc - 1);
std::string link_name = link_tag.substr(second_space_loc + 1);
// Adds the link as a pair to a vector of links.
v_links_.push_back({file_name, link_name});
// Reformat the link tag to match specification.
line.replace(tag_loc, tag_len + 1, "<" + link_name + ">[" + to_string(curr_link) + "]");
}
// Search for all paragraphs in the line and add them to the v_lines vector.
for(auto p_tag = line.find("<p>"); p_tag != -1; p_tag = line.find("<p>"))
{
std::string extra_text = line.substr(p_tag + 4);
// Seperate out text that should stay in the previous paragraph and text that goes in the new paragraph. Delete the tag in the process.
std::string extra_text = line.substr(p_tag + 3);
line.erase(p_tag);
curr_p += line;
// However, if the paragraph is empty, then it shouldn't be added.
// However, if the paragraph tag is located at the start of the file, there will be no data, so it shouldn't be added as it will be an empty line.
if(curr_p != "")
{
v_lines_.push_back(curr_p);
curr_p = "";
}
// Move any remaining data on the line back into the line variable so that it can be processed.
line = extra_text;
}
// Append any data left on this line to the current paragraph being read.
// Append any data left on this line to the current paragraph being read. Add a space so that content from two lines can be seperated.
curr_p += " " + line;
}

10
test.txt

@ -1,6 +1,6 @@
<p> This is the first line in the file.
<p> This is the second <br> and third line of the file.
<p> This line has a link to the <a docs/specification.txt specification> file.
<p> This line is split into <p> two paragraph tags.
<p> This line contains some<br> problematic<p> tags.
<p>This is the first line in the file.
<p>This is the second <br> and third line of the file.
<p>This line has a link to the <a docs/specification.txt specification> file.
<p>This line is split into <p>two paragraph tags.
<p>This line con<br>tains some<br > problematic< p> tags.
Derp. Derp. Derp. Derp. Derp.
Loading…
Cancel
Save