Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: write html code with perl

by kcott (Archbishop)
on Mar 01, 2014 at 06:30 UTC ( [id://1076645]=note: print w/replies, xml ) Need Help??


in reply to write html code with perl

G'day lenieto3,

I created an images directory with these dummy files:

asd_fgh_jkl.png qwerty.png uiop.png zxcv_bnm.png

This script:

#!/usr/bin/env perl use strict; use warnings; my $ul = 0; while (<DATA>) { $ul = 0 if /<\/ul>/; next if $ul; print; if (/<ul>/) { $ul = 1; for (glob 'images/*.png') { my @name = map { "\u$_" } split /_/ => (/images\/(.+)\.png +/)[0]; print qq{ <li><a target="_blank" href="$_">@nam +e</a></li>\n}; } } } __DATA__ <html> <head> <link type="text/css" rel="stylesheet" href="style/stylesheet. +css"/> <title>mchtd.info</title> </head> <body> <ul> <li><a target="_blank" href="images/patecumbia.png">Patecu +mbia</a></li> <li><a target="_blank" href="images/trapped.png">Trapped</ +a></li> <li><a target="_blank" href="images/navaja_force.png">Nava +ja Force</a></li> <li><a target="_blank" href="images/que_esta_pasando.png"> +&iquest;Qu&eacute; est&aacute; pasando?</a></li> <li><a target="_blank" href="images/the_look.png">The Look +</a></li> <li><a target="_blank" href="images/hablame.png">Hablame</ +a></li> </ul> </body> </html>

Produced this output:

<html> <head> <link type="text/css" rel="stylesheet" href="style/stylesheet. +css"/> <title>mchtd.info</title> </head> <body> <ul> <li><a target="_blank" href="images/asd_fgh_jkl.png">Asd F +gh Jkl</a></li> <li><a target="_blank" href="images/qwerty.png">Qwerty</a> +</li> <li><a target="_blank" href="images/uiop.png">Uiop</a></li +> <li><a target="_blank" href="images/zxcv_bnm.png">Zxcv Bnm +</a></li> </ul> </body> </html>

That's pretty much what you want except for link names like "&iquest;Qu&eacute; est&aacute; pasando?": you'd need to set up rules for those; if there's only a few, manual editing may be less effort than writing the code.

-- Ken

Replies are listed 'Best First'.
Re^2: write html code with perl
by graff (Chancellor) on Mar 01, 2014 at 09:11 UTC
    Hi Ken. Regarding your closing remark about character entities for the non-ASCII characters, I think one rule will suffice: use HTML::Entities. It has an "encode_entities" function that can convert utf-8 characters in a given range to their "symbolic" entity-references:
    perl -MHTML::Entities=encode_entities -e 'print encode_entities("\x{00 +bf}Que?")'

    (Update: to clarify: if the files to be listed already have quaint names with non-ASCII utf8 characters in them, then adding HTML::Entities::encode_entities will make sure those names will display correctly without having to worry about a given browser's default character-set; but if the string descriptions to be displayed are different from the actual file names used, then there would need to be some sort of look-up table for that.)

      "Regarding your closing remark about character entities for the non-ASCII characters, I think one rule will suffice: use HTML::Entities. ..."

      Use of HTML::Entities was my first thought too; however, the one line that didn't follow the general pattern seemed so different, that I decided to suggest special rules or manual editing.

      For all lines (except the que_esta_pasando.png one), the "my @name = ..." line seemed to embody the general pattern to the extent possible from the code provided by the OP: trapped.png becomes Trapped, the_look.png becomes The Look, and so on.

      For the que_esta_pasando.png line, the general pattern is not applied beyond splitting on underscores. I think your "Update" alludes to this but I thought I'd point out the differences, as I see them, in this specific case.

      • Split on underscores (as for other lines): "que_esta_pasando.png" becomes "que esta pasando"
      • Know this is a foreign language. [I've assumed Spanish; happy to be corrected; it doesn't change the point I'm making.]
      • Apply the appropriate diacritical marks for this language: "que esta pasando" becomes "qué está pasando"
      • Treat as a sentence, not as a title, so only the first word is capitalised: "qué está pasando" becomes "Qué está pasando"
      • Know enough of this language to understand that this sentence is a question, not a statement. Apply the appropriate punctuation for a question in this language: "Qué está pasando" becomes "¿Qué está pasando?"
      • Not shown, but adding a lang="es" attribute would probably also be appropriate.

      All of this could be coded and, for some massive international image library with ongoing maintenance requirements, may well be the way to go. However, the OP states "I'm working in a basic html page, actually it is just a list of links to some pictures.": in this instance, rules (such as a lookup table which you've suggested) or manual editing seems more sensible and a lot less work.

      -- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1076645]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-16 05:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found