SPUG: Re: Perl and HTML

Ken McGlothlen mcglk at serv.net
Thu Oct 26 05:59:39 CDT 2000


I've been thinking about this a lot over the last year.  I continually find
myself frustrated with webpage generation, and I wish there was a good way to
meld what is a loose collection of partial solutions together.

First of all, using HTML as a source language is a dead-end route.  That would
be fine if all the browsers out there supported XML, but they don't; instead,
people are using browsers that support bits and subsets of HTML, often in
different ways.  Shifting to XML as a source language makes it easier to
support future browsers, most of which will (hopefully) be adhering fairly
closely to the standards.

Second, server-side manipulation isn't a great answer in all cases, either.
The majority of content on the Web is static in nature, and speed is of the
essence for most websites.  In addition, many small sites can't really afford a
lot of custom services by their ISPs in order to support server-side processing
(AxKit, etc.).

Finally, XML isn't a great tool for certain things.  Character entities are
a pain; there should be a way to perform text substitutions conveniently (which
would also be useful for other things).  And sometimes, there needs to be some
programmatic control that XML/XSLT doesn't cover (like "current time").  It's
great for static replacements, but doesn't provide all that much control over
non-tag content.

The sad thing is that this sort of thing was covered a long time ago by various
text-processing systems.  Take TeX, for example:  flexible, precise, and a full
macro language to boot.

So why isn't there a true XML macro language by now?  One that could be plugged
into Apache, for example, if you needed on-the-fly conversion (ecommerce,
games, etc.), but one that can be run independently as a compiler for those
that run small static websites?

Now, pardon me if there are any errors in here---I'm writing this off the top
of my head, and it is nearly 4am as I write this---but wouldn't it be nice if
we could write

	<?xml version = "1.0"?>
	<page-faqlist>
	<title>Not-so-frequently asked questions</title>
	<qapair>
	<q>Would you say, ``Is it farther to Spokane or by bus~24?''</q>
	<a>No---in general, the sun has no half-tildes \<1/2 \~s\>.</a>
	</qapair>
	</page-faqlist>

with the following DTD for syntax checking:

	<!ELEMENT page-faqlist (title, comment*, qapair*)>
	<!ELEMENT title (#PCDATA)*>
	<!ELEMENT comment (#PCDATA)*>
	<!ELEMENT qapair (q, a)>
	<!ELEMENT q (#PCDATA)*>
	<!ELEMENT a (#PCDATA)*>

and the following .xsl file for processing:

	<?xml version = "1.0">
	<xsl:transform xmlns:xsl = "http://www.w3.org/XSL/Transform"
		version = "1.0">
	<xsl:perlrun>
		use POSIX;
	</xsl:perlrun>
	<xsl:textsubsdef name = "normal">
		<xsl:subs match = "\\" replace = "&#0092;"/>
		<xsl:subs match = "\<" replace = "&lt;"/>
		<xsl:subs match = "\>" replace = "&gt;"/>
		<xsl:subs match = "\&" replace = "&amp;"/>
		<xsl:subs match = "1/2" replace = "&#0189;"/>
		<xsl:subs match = "\~" replace = "&#0126;"/>
		<xsl:subs match = "~" replace = "&nbsp;"/>
		<xsl:subs match = "\-" replace = "&#0045;"/>
		<xsl:subs match = "---" replace = "&#0151;"/>
		<xsl:subs match = "--" replace = "&#0150;"/>
		<xsl:subs match = "\`" replace = "&#0096;"/>
		<xsl:subs match = "\'" replace = "&#0039;"/>
		<xsl:subs match = "```" replace = "&#0147;&#0145;"/>
		<xsl:subs match = "'''" replace = "&#0146;&#0148;"/>
		<xsl:subs match = "``" replace = "&#0147;"/>
		<xsl:subs match = "''" replace = "&#0148;"/>
		<xsl:subs match = "`" replace = "&#0145;"/>
		<xsl:subs match = "'" replace = "&#0146;"/>
	</xsl:textsubsdef>
	<xsl:perlsub name = "dateofthisfile">
		my( $filename ) = shift;
		return( strftime( "%Y/%M/%D",
			localtime( (stat( $filename ))[9] ) ) );
	</xsl:perlsub>
	<xsl:template match = "page-faqlist">
		<html><head><title><xsl:value-of select = "title"/></title>
		<body bgcolor = "#ffffff">
		<xsl:usetextsubs name = "normal">
		<h2><font face = "Arial,Helvetica"><xsl:value-of select =
			"title"/></font></h2>
		<xsl:apply-templates/>
		<hr>
		<p>Last modified:
		<xsl:callperl name = "dateofthisfile" args = "_XMLFILE_"></p>
		</xsl:usetextsubs>
		</body></html>
	</xsl:template>
	<xsl:template match = "q">
		<p><helv><b>Q:</b> <xsl:apply-templates/></helv></p>
	</xsl:template>
	<xsl:template match = "a">
		<p><helv><b>A:</b> <xsl:apply-templates/></helv></p>
	</xsl:template>
	<xsl:template match = "comment">
		<table width = "100%" cellpadding = "10" cellspacing = "0"
		    border = "0" bgcolor = "#ffd0d0"><tr valign = "top">
		    <td width = "10%">&nbsp;</td>
		    <td width = "90%"><helv><xsl:apply-templates/></helv></td>
		</tr></table>
	</xsl:template>
	<xsl:template match = "helv">
		<font face = "Arial,Helvetica"><xsl:apply-templates/></font>
	</xsl:template>

and be able to type a simple command and get the resulting HTML?

This sort of approach might just make Perl the preferred language for XML
processing, since it'd be fully macroizable, and also make it not too far off
the standards for XML/XSLT.

I dunno; maybe it's not such a good idea, but I yearn for this sort of
flexibility.  For example, I'd love to have something like small caps, which
translates the following (assuming the default font size here):

	<sc>Seattle Perl Users Group</sc>

to

	S<font size = "-1">EATTLE</font> P<font size = "-1">ERL</font>
	U<font size = "-1">SERS</font> G<font size = "-1">ROUP</font>

Or even more complex macros.

I'd love to see SPUG involved in something like this.  Between XML::Parser,
subroutine references, eval, and XML::XSLT, this really shouldn't be all that
hard, should it?  And I suspect that a tool like this would get used.



 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/





More information about the spug-list mailing list