inicio
mail me!
sindicaci;ón
 
N
E
W
B
I
E
 
G
A
M
E
 
P
R
O
G
R
A
M
M
E
R
S
 

Archive for March, 2008

Using Perl and Regular Expressions to Process Html Files - Part 2

Author: John Dixon

In this article we will discuss how to change the contents of an HTML file by running a Perl script on it.

The file we are going to process is called file1.htm:

Note: To ensure that the code is displayed correctly, in the example code shown in this article, square brackets ‘[..]’ are used in HTML tags instead of angle brackets ”.

[html]
[head][title]Sample HTML File[/title]
[link rel=”stylesheet” type=”text/css” href=”style.css”]
[/head]
[body]
[h1]Introduction[/h1]
[p]Welcome to the world of Perl and regular expressions[/p]
[h2]Programming Languages[/h2]
[table border=”1″ width=”400″]
[tr][th colspan=”2″]Programming Languages[/th][/tr]
[tr][td]Language[/td][td]Typical use[/td][/tr]
[tr][td]JavaScript[/td][td]Client-side scripts[/td][/tr]
[tr][td]Perl[/td][td]Processing HTML files[/td][/tr]
[tr][td]PHP[/td][td]Server-side scripts[/td][/tr]
[/table]
[h1]Summary[/h1]
[p]JavaScript, Perl, and PHP are all interpreted programming languages.[/p]
[/body]
[/html]

Imagine that we need to change both occurrences of [h1]heading[/h1] to [h1 class=”big”]heading[/h1]. Not a big change and something that could be easily done manually or by doing a simple search and replace. But we’re just getting started here.

To do this, we could use the following Perl script (script1.pl):

1 open (IN, “file1.htm”);
2 open (OUT, “>new_file1.htm”);
3 while ($line = [IN]) {
4 $line =~ s/[h1]/[h1 class=”big”]/;
5 (print OUT $line);
6 }
7 close (IN);
8 close (OUT);

Note: You don’t need to enter the line numbers. I’ve included them simply so that I can reference individual lines in the script.

Let’s look at each line of the script.

Line 1
In this line file1.htm is opened so that it can be processed by the script. In order to process the file, Perl uses something called a filehandle, which provides a kind of link between the script and the operating system, containing information about the file that is being processed. I’ve called this “opening” filehandle ‘IN’, but I could have used anything within reason. Filehandles are normally in capitals.

Line 2
This line creates a new file called ‘new_file1.htm’, which is written to by using another filehandle, OUT. The ‘>’ just before the filename indicates that the file will be written to.

Line 3
This line sets up a loop in which each line in file1.htm will be examined individually.

Line 4
This is the regular expression. It searches for one occurrence of [h1] on each line of file1.htm and, if it finds it, changes it to [h1 class=”big”].

Looking at Line 4 in more detail:

  • $line - This is a variable that contains a line of text. It gets modified if the substitution is successful.
  • =~ is called the comparison operator.
  • s is the substitution operator.
  • [h1] is what needs to be substituted (replaced).
  • [h1 class=”big”] is what [h1] has to be changed to.

Line 5
This line takes the contents of the $line variable and, via the OUT file handle, writes the line to new_file1.htm.

Line 6
This line closes the ‘while’ loop. The loop is repeated until all the lines in file1.htm have been examined.

Lines 7 and 8
These two lines close the two file handles that have been used in the script. If you missed off these two lines the script would still work, but it’s good programming practice to close file handles, thus freeing up the file handle names so they can be used, for example, by another file.

Running the Script

As the purpose of this article is to explain how to use regular expressions to process HTML files, and not necessarily how to use Perl, I don’t want to spend too long describing how to run Perl scripts. Suffice to say that you can run them in various ways, for example, from within a text editor such as TextPad, by double-clicking the perl script (script1.pl), or by running the script from an MS-DOS window.

(The location of the Perl interpreter will need to be in your PATH statement so that you can run Perl scripts from any location on your computer and not just from within the directory where the interpreter (perl.exe) itself is installed.)

So, to run our script we could open an MS-DOS window and navigate to the location where the script and the HTML file are located. To keep life simple I’ve assumed that these two files are in the same folder (or directory). The command to run the script is:

C:>perl script1.pl

If the script does work (and hopefully it will), a new file (new_file1.htm) is created in the same folder as file1.htm. If you open the file you’ll see the the two lines that contained [h1] tags have been modified so that they now read [h1 class=”big”].

In Part 3 we’ll look at how to handle multiple files.

About the Author:
John is a web developer working for My Health Questions Matter, a company dedicated to helping patients to get the most out of their interaction with health care professionals such as doctors, midwives, and consultants by generating a set of health questions a patient can ask at an appointment.

Using Perl and Regular Expressions to Process Html Files - Part 1

Author: John Dixon

Like many web content authors, over the past few years I’ve had many occasions when I’ve needed to clean up a bunch of HTML files that have been generated by a word processor or publishing package. Initially, I used to clean up the files manually, opening each one in turn, and making the same set of updates to each one. This works fine when you only have a few files to fix, but when you have hundreds or even thousands to do, you can very quickly be looking at weeks or even months of work. A few years ago someone put me on to the idea of using Perl and regular expressions to perform this ‘cleaning up’ process.

Why write an article about Perl and regular expressions I hear you say. Well, that’s a good point. After all the web is full of tutorials on Perl and regular expressions. What I found though, was that when I was trying to find out how I could process HTML files, I found it difficult to find tutorials that met my criteria. I’m not saying they don’t exist, I just couldn’t find them. Sure, I could find tutorials that explained everything I needed to know about regular expressions, and I could find plenty of tutorials about how to program in Perl, and even how to use regular expressions within Perl scripts. What I couldn’t find though, was a tutorial that explained how to open one or more HTML or text files, make updates to those files using regular expressions, and then save and close the files.

The Goal

When converting documents into HTML the goal is always to achieve a seamless conversion from the source document (for example, a word processor document) to HTML. The last thing you need is for your content authors to be spending hours, or even days, fixing untidy HTML code after it has been converted.

Many applications offer excellent tools for converting documents to HTML and, in combination with a well designed cascading style sheet (CSS), can often produce perfect results. Sometimes though, there are little bits of HTML code that are a bit messy, normally caused by authors not applying paragraph tags or styles correctly in the source document.

Why Perl?

The reason why Perl is such a good language to use for this task is because it is excellent at processing text files, which let’s face it, is all HTML files are. Perl is also the de facto standard for the use of regular expressions, which you can use to search for, and replace/change, bits of text or code in a file.

What is Perl?

Perl (Practical Extraction and Report Language) is a general purpose programming language, which means it can be used to do anything that any other programming language can do. Having said that, Perl is very good at doing certain things, and not so good at others. Although you could do it, you wouldn’t normally develop a user interface in Perl as it would be much easier to use a language like Visual Basic to do this. What Perl is really good at, is processing text. This makes it a great choice for manipulating HTML files.

What is a Regular Expression?

A regular expression is a string that describes or matches a set of strings, according to certain syntax rules. Regular expressions are not unique to Perl - many languages, including JavaScript and PHP can use them - but Perl handles them better than any other language.

In part 2, we’ll look at our first example Perl script.

About the Author:
John is a web developer working for My Health Questions Matter, a company dedicated to helping patients to get the most out of their interaction with health care professionals such as doctors, midwives, and consultants by generating a set of health questions a patient can ask at an appointment.

Top of the Top - Wordpress Themes

Top 50 Free Wordpress Theme (Excellent article, has screen shots of each theme, with a link to a demo, and a download link)

100 Excellent Free Wordpress Themes (Great article on 100 Free Wordpress themes, shows screen shots of some)

20 More Free First-Class Wordpress Themes

83 Beautiful Wordpress Themes

10 Fresh and Clean Themes

10 Unusual & Original Wordpress Themes

21 Fresh, Usable and Elegant Themes

Top Wordpress Themes (Great site, split up into interesting categories such as 2-column, 3-column, widget ready, etc)

My Top Thirty Wordpress Themes

Best 3-Column Wordpress Themes

The Best Minimalist Wordpress Themes

Top Ten Best Free Wordpress Themes - Part 1 :: Part 2 :: Part 3

10+ Free Magazine Style WordPress Themes to Choose From

Top 50 Rated Wordpress Themes

Best Wordpress Themes on Squidoo

Top 20 WordPress Themes of 2007

10 Best Web 2.0 Wordpress Themes

This last bunch is from Hackwordpress.com

  1. Premium WordPress Themes Gallery
  2. 2-Column WordPress Theme Gallery
  3. 3-Column WordPress Theme Gallery
  4. 4-Column WordPress Theme Gallery
  5. K2 WordPress Theme Mods and Styles Gallery
  6. Google AdSense Optimized WordPress Theme Gallery
  7. Search Engine Optimized (SEO) WordPress Theme Gallery
  8. Magazine Style WordPress Theme Gallery
  9. 125×125 Button-Ready WordPress Themes Gallery

Street Fighter IV Trailer

One of my favourite games when I was a young’n was Street Fighter, yes I am old enough to have played Street Fighter One. But Street Fighter 2 was the game I wasted most of my money on. I found a video of the upcoming Street Fighter 4 game for the next gen systems. Looks nice, but seems like they just slapped on 3-D’ish graphics on to the same games, with a couple more special moves. But we will see, still can’t wait to see it.



Street Fighter IV `AOU` Trailer