SHOP PRODUCTS
Houzz Logo Print
chuggerguy

bbcode2html

A post elsewhere got me playing around with the stream editor sed in a bash script trying to convert existing text files containing bbcode into text files containing html code.

So, despite probable easier pre-made solutions, seeing it as an opportunity to learn at least a little sed/regex, I decided to throw something together, just for the fun of it.

Assume there exists a collection of "how-to" type text files, used as templates to copy/paste as needed, and containing a few bbc tags. Not too many though, I don't want to spend too much time on this.

The setup:
{{gwi:1539422}}

You have a folder containing the necessary script files and a directory full of "how-to" text files containing bbc tags. Ordinarily you post the how-tos in forums where bbcode is used, but you'd also like to occasionally make use of them on a site that uses html. You have too many to manually convert or even to use an online converter. So you've decided to try batch converting them. Of course, these are not your only copies, just in case. :)

The contents of the two script files...

bbc2html.sed:
{{gwi:1539423}}

convertALL:
{{gwi:1539424}}

The first file does the substitutions using sed, the second file passes all the text(.txt) files in the toConvert directory to/through the sed file and saves the result in a directory(created if it doesn't exist) called "converted" using a slightly modified name to indicate they now contain html. Hopefully they do... although with my limited expertise I'd not count on it. I suspect the input files had better not contain invalid(improper nesting?) bbcode or anything too complex.

Using this text file as a test input file

{{gwi:1539425}}

and executing "convertALL" creates a folder named "converted" and within that folder is a newly created file called testingAfewTags.html.txt.

The contents of the testingAfewTags.html.txt:
{{gwi:1539426}}

Posting the result here on gardenweb.com gives:

bold

italic

underline

strikethrough

centered text

http://google.com ...

Comments (4)

Sponsored
Winks Remodeling & Handyman Services
Average rating: 5 out of 5 stars1 Review
Custom Craftsmanship & Construction Solutions in Franklin County