Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

[ASCII 2 HTML] Appearently unable to find simple module!

by blazar (Canon)
on Nov 30, 2008 at 12:21 UTC ( #726893=perlquestion: print w/ replies, xml ) Need Help??
blazar has asked for the wisdom of the Perl Monks concerning the following question:

I personally believe that this morning I must be even more dumb than usual, because I seem to be completely unable to find a supposedly simple but not entirely trivial module suitable for my needs and that IMHO should be out there...

Specifically, I have some pure text chunks: code, preformatted text, ASCII art and so on. Now, for visualization purposes, I'd like not to print them to a terminal but into an HTML file, presumably within <pre> tags, but also possibly further embedded in a <div> complete with a caption, like as if it were a figure, and so on.

To put it briefly, something akin to what PM's code tags themselves do, except that I do not need the automatic wrap on long lines, which would even be annoying in my case, but that's just a detail... Of course and to put it briefly, there's nothing in this that I could not concoct up myself, but as a matter of principle it seems strange to me that there's not a module that would simply allow me to do something like:

print $t2h->add(text => $font->figify(-A => 'Hello, World!'), caption => "Text::FIGlet example, with font '$fontname +'");

I noticed I could find stuff just as complex as HTML::FromANSI which in turn looks like an overkill for what I'm really after. Thus the actual questions are:

  • do such modules exist?
  • Which one would you recommend?
  • Which cpan search would you suggest to locate them, since I appearently couldn't find a very good one?
--
If you can't understand the incipit, then please check the IPB Campaign.

Comment on [ASCII 2 HTML] Appearently unable to find simple module!
Select or Download Code
Re: [ASCII 2 HTML] Appearently unable to find simple module!
by Corion (Pope) on Nov 30, 2008 at 13:21 UTC

    I'm not sure if it does what you mean, but shouldn't the following do what you need?

    use HTML::Entities; my $ascii = <<'ASCII'; The quick brown fox jumps over the lazy god. ASCII; my $html = encode_entities($ascii); $html =~ s/\r?\n/<br>/g; print "<pre>$html</pre>";

    I'm unaware of any module doing that in its entirety, but maybe you want to just serve up the ascii page with Content-Type: text/plain instead of Content-Type: text/html ?

      I personally believe that it does exactly what I need, except that as I wrote in the root node I would probably further wrap the the <pre> tags in <div> ones, and that's what I meant with "concocting up a solution of my own." (I can't remember the exact expression I used, but that's not fundamental...)

      As a side note, it's not a matter of serving the page in any particular way: I'm just generating a standalone HTML single file for visualization purpose playing a little with css settings. It's not really a webby thing if you know what I mean...

      --
      If you can't understand the incipit, then please check the IPB Campaign.
Re: [ASCII 2 HTML] Appearently unable to find simple module!
by oko1 (Deacon) on Nov 30, 2008 at 13:59 UTC

    Perhaps I'm missing something, but what's wrong with the plain old CGI module?

    #!/usr/bin/perl -w use strict; use CGI::Carp qw/fatalsToBrowser warningsToBrowser/; use CGI qw/:standard/; $|++; my $fontname = "italic 1em Arial, sans-serif"; open F, '>', "output.html" or die "output.html: $!\n"; print F start_html, div({ style => "font: $fontname;" }, "\n", pre("Hello, World!"), "\n", pre("CGI example, with font '$fontname'") ), end_html;

    Update: Removed the call to 'header' from the print statement; it's unnecessary if you're writing to a file, of course.

    Update2: Since I'd used literal text strings, I'd forgotten about converting various characters to HTML entities (thanks, Corion!) This is easily done either via HTML::Entities or possibly by processing the text through something like this:

    my %s; @s{split //, '<>&'} = qw/lt gt amp/; my $text = do { local $/; <Input> }; $text =~ s/([<>&])/"&$s{$1};"/ges;

    --
    "Language shapes the way we think, and determines what we can think about."
    -- B. L. Whorf
Re: [ASCII 2 HTML] Appearently unable to find simple module!
by ww (Bishop) on Nov 30, 2008 at 14:41 UTC
    Though it doesn't appear to do all you want, http://search.cpan.org/~bpostle/MKDoc-Text-Structured-0.83/lib/MKDoc/Text/Structured.pm may provide a start. OTOH, a search for "text<" produced at least 8 pages of results, including the likes of http://search.cpan.org/~danberr/Text-Smart-1.0.2/lib/Text/Smart/HTML.pm.

    Alternately, does this render as you wish?

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:/ +/www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Blazar's Intent?</title> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"> <!-- .text { width: 100em; max-width: 100em; color: black; background-color: white; } .textshorter { width: 80em; max-width: 80em; } #art { color: green; background-color: white; } .table { margin-left: 5em; border: medium solid black; font-style: italic; width: 16em; max-width: 40em; } --> </style> </head> <body> <p class="text"> Now is the time to convert this very long ascii text to html without c +ontinuation lines. Note that this works only within whatever you defi +ne as "reasonable" bounds for screen width or accept possibly-unaccep +table horizontal scrolling in the enduser's browser. </p> <p class="textshorter"> This is enclosed by a &lt;div class="textshorter&gt; Now is the time t +o convert this very long ascii text to html without continuation line +s. Note that this works only within whatever you define as "reasonabl +e" bounds for screen width or accept possibly-unacceptable horizontal + scrolling in the enduser's browser. </p> <p class="textshorter"> Next comes some ascii art: </p> <pre id="art"> * ^ / \ */ \ / \* / \ *----|---- | </pre> <pre class="text"> And a table (less the inter-cell borders): </pre> <pre class="table"> Foo Bar Blivitz 1 0 1 0 1 0 379 4 2816 </pre> </body> </html>

    If so, even though this ignores many edge cases and non-trivial issues, but it seems to me that writing a module to do (more or less) what this does would be unnecessary if your project can tolerate a requirement for some minimal markup or if one applies some fairly straightforward heuristics to templating the original text.

Re: [ASCII 2 HTML] Appearently unable to find simple module!
by planetscape (Canon) on Dec 01, 2008 at 03:59 UTC

    I get several results when I search for "ascii2html perl" or "txt2html perl". Perhaps one of these does what you want, or can be easily modified?

    HTH,

    planetscape

      I personally believe that Google is always a good resource, but a very generic one as well. I did hope in a CPAN-ready specific one instead. Eventually I did what I should probably have done in the first place: roll my own solution - not sure whether it came out better or worse than the hits you found searching; but that's in the eye of the beholder, and what's important in this sense is that I got what I needed. So end of story! Thank you anyway.

      --
      If you can't understand the incipit, then please check the IPB Campaign.
Re: [ASCII 2 HTML] Appearently unable to find simple module!
by scorpio17 (Monsignor) on Dec 01, 2008 at 15:30 UTC

    I've done similar stuff using HTML::Template. You make a wrapper template that contains your formatting: styles, div tags, pre tags, etc., and in the middle you have one special template variable intended to hold your ascii art, then you write a cgi script to load the template, slurp in the ascii file, stuff it into the template, then print it out to the browser.

    Since templates can contain other templates, this technique works well anytime you want to build a "widget" of some kind (a little block of HTML that isn't really a page by itself, but that may get used over and over).

    For example, if you wanted to build a photogallery web page, you could build a little template for formatting a single thumbnail image with a caption centered below it. Then, your main script could search a directory for thumbnail images and loop over each, building up the main page dynamically by assembling all the little blocks of html generated by the template.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://726893]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2014-10-21 03:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (95 votes), past polls