Results 1 to 2 of 2
Hi!
I am trying to write a bash script that will go through (many) text file(s) and do this:
== Something == converts to <h2> Something </h2>
= Something = ...
- 03-02-2009 #1Just Joined!
- Join Date
- Mar 2008
- Posts
- 12
Custom text to html - bash
Hi!
I am trying to write a bash script that will go through (many) text file(s) and do this:
== Something == converts to <h2> Something </h2>
= Something = converts to <h1> Something </h1>
* line 1
* line 2
* line 3
converts to
<ul>
<li> line 1 </li>
<li> line 2 </li>
<li> line 3 </li>
</ul>
and adds <br/> to every empty line
And then save it as a .html file.
I have no idea how to do this, any help would be really appreicated!
Thanks!
- 03-02-2009 #2Just Joined!
- Join Date
- Feb 2009
- Posts
- 45
Answer:
This is easily achievable by using «Perl» in the following manner:
Originally Posted by Zata
where “convert.pl” contains the following:Code:for txt_file in $LIST_OF_TXTFILES; do cat "$txt_file" | convert.pl > "${txt_file/.txt/.html}";¹ done
If this perl file is applied to the textCode:#! /usr/bin/perl -CASD ## …… Uses …… use warnings; use strict; use utf8; ## …… Functions …… sub printbr { print "$_[0]<br />\n" } ## …… Globals …… my @line_stack; ## …… Code …… while (defined (my $line = <>)) { chomp $line; $line =~ s{== (.*?) ==}{<h2> $1 </h2>}; $line =~ s{= ([^=]*?) =}{<h1> $1 </h1>}; if ($line =~ /^\s*\* (.*)$/) { push @line_stack, $1; } else { if (@line_stack) { printf "<ul>\n%s</ul>\n", join '', map { "\t<li> $_ </li>\n" } @line_stack; @line_stack = (); } if ($line =~ m!</h1>$! or $line =~ m!</h2>$!) { print "$line\n"; } else { printbr $line; }}} if (@line_stack) { printf "<ul>\n%s</ul>\n", join '', map { "\t<li> $_ </li>\n" } @line_stack; }
it yieldsCode:Hello world This is a random text. = Nice Heading = == Chapter 1 == This is a highly random text. In fact one could have used “fortune” to create it. But thatʼs just useless. Hereʼs a list of what todo to make the world a better place instead: * Buy highly inflammable materials * Buy lighter * Burn down the world * Erect a new kingdom from its ashes == Chapter 2 == Well, that wasnʼt so successful now, wasnʼt it ? == Chapter 3 == $> fortune It is better for civilization to be going down the drain than to be coming up it. -- Henry Allen == Chapter 4 = ups? intended? = == Ok, enough for today… Well, one more list: * Sudo * Make * Me * A * Sandwich
If there are any questions regarding the code, feel free to ask. Blindly applying code one doesnʼt understand is rarely considered a good idea.Code:Hello world<br /> <br /> This is a random text.<br /> <br /> <h1> Nice Heading </h1> <h2> Chapter 1 </h2> This is a highly random text.<br /> In fact one could have used “fortune”<br /> to create it.<br /> But thatʼs just useless.<br /> Hereʼs a list of what todo to make<br /> the world a better place instead:<br /> <ul> <li> Buy highly inflammable materials </li> <li> Buy lighter </li> <li> Burn down the world </li> <li> Erect a new kingdom from its ashes </li> </ul> <h2> Chapter 2 </h2> Well, that wasnʼt so successful now,<br /> wasnʼt it ?<br /> <h2> Chapter 3 </h2> $> fortune<br /> It is better for civilization to be<br /> going down the drain than to be<br /> coming up it.<br /> -- Henry Allen<br /> <h2> Chapter 4 <h1> ups? intended? </h1> </h2> Ok, enough for today…<br /> Well, one more list:<br /> <ul> <li> Sudo </li> <li> Make </li> <li> Me </li> <li> A </li> <li> Sandwich </li> </ul>
Footnotes:
- This piece of code assumes all your text files possess the file ending “.txt”. Thus the perl script is applied to each text file and the result is saved in “$txt_filename.html”.


Reply With Quote