Bashing izzaboo with a Blog

tilde.club/~izzaboo via BashBlog

Parsing Once, Parsing Twice...

November 20, 2014 — Greg Haas

As I said before, I still have a lot to learn!

I'm learning more about bash scripting, as I'd hoped! I've been working with a javasript/PHP combo that reads flat text files and generates html dynamically. I'm thinking something like bashblog could be used to read from an uploaded text file or of the temporary text output from $EDITOR. I'm imagining feeding the command "convert" instead of "post". So one would enter:

shell$: ./bb.sh convert -html /home/user/www/files/file1.txt.

Or would it bet better to add an argument to edit or post command? Such as

shell$: post -t -html ~/www/files/*.txt
?

I don't know if it is any good, but I've been working with custom markup, since all the files are of very simple and consistent format: Right aligned file code, centered file title in H3, paragraphs, two-level lists, sub-headings, left aligned in H4 and some bold characters.

for (var i = 0; i < numLines; i++){
  if (lines[i].lastIndexOf('~~>',0) === 0){
    parsedData += "<p  class=\"text-right\">"+lines[i].slice(3)+"</p>";
  }else if(lines[i].lastIndexOf('===', 0) === 0){
    parsedData += "<h4>"+ lines[i].slice(3)+"</h4>";
  }else if(lines[i].lastIndexOf('==',0) === 0){
    parsedData += "<h3>"+ lines[i].slice(2)+"</h3>";
  }else if(lines[i].lastIndexOf('***',0) === 0){
    parsedData += "<b style=\"font-size: .8em;\">"+lines[i].slice(3)+"</b>";
  }else if(lines[i].lastIndexOf('~~~',0) === 0){
    parsedData += "<p class=\"text-justify\">"+ lines[i].slice(3)+"</p>";
  }else if(lines[i].indexOf('    ') === 0){
    parsedData += "<li style=\"margin-left: 2.2em;list-style-type: none;\">"
                +lines[i].slice(4))+"</li>";
  }else if(lines[i].indexOf('  ') === 0){
    parsedData += "<li style=\"margin-left: 1.2em;list-style-type: none;\">"
                +lines[i].slice(2)+"</li>";
  }else{
    parsedData += "<p>"+ lines[i]+"</p>";
  }
}
return parsedData;

That's a lotta if-else pudding. But, good JS or not, that ought to be fairly easy to do with bash scripting, right? Easier, maybe?

The other piece is the file opening and reading. Right now I'm doing ajax requests to a php script which is returning the string to be parsed by javascript. I use ajax because there are several series, or groupings of files, then the files themselves. I put each grouping in a directory. One <select> menu chooses the folder, the next menu chooses the .txt file and javascript splats it into innerHTML.

Feels very much like a kludge and although it works, I'd like it to be more elegant. I don't know. I'd also like it searchable on the front end...

I quickly get bogged down and figure why reinvent the wheel? Well, maybe because all the wheels I can find are the wrong size.

*sigh* Learning learning learning. This may be all that's keeping me from getting unhealthy.

Comments? Tweet