Aldebaran has asked for the wisdom of the Perl Monks concerning the following question:

I present today with a grab bag of problems, that I can demonstrate in some type of a short and closed example. I've gotten some form of reasonable output with my russian crosswords, but what should align does not yet. Let me show the page, and if you're at all interested in making this easier on the eyes, then click readmore. ataman is the page, and the project is on github at first russian xword

To fix this, I have to rewrite:

sub get_utf8_text { use 5.010; use HTML::FromText; use Path::Tiny; use utf8; use open OUT => ':utf8'; ### Passing in #reference to main data structure and directory for captions my ( $rvars, $dir ) = (@_); my %vars = %$rvars; say "dir is $dir"; opendir my $eh, $dir or warn "can't open dir for utf8 captions $!\n +"; while ( defined( $_ = readdir($eh) ) ) { next if m/~$/; next if -d; if (m/txt$/) { my $file = path( $dir, $_ ); my $guts = $file->slurp_utf8; my $temp = text2html( $guts, lines => 1, paras => 1, ); # surround by divs my $oitop = $vars{"oitop"}; my $oben = $oitop->slurp_utf8; my $oibottom = $vars{"oibottom"}; my $unten = $oibottom->slurp_utf8; my $text = $oben . $temp . $unten; say "text is $text"; $content{$_} = $text; } } closedir $eh; #important to sort my @return; foreach my $key ( sort keys %content ) { #print $content{$key} . "\n"; push @return, $content{$key}; } return \@return; }

I think I want to get away from HTML::FromText for this input, as I will have a reference to the array of array of at hand. I am not, however, certain that I'm not getting the exact representation I need, but that my .css is off.

Here is what the body looks like, both on STDOUT and when I view the page source:

<div class="outer"> <div class="hft-lines">&#x43B; &#x43B;&#x435;&#x441;&#x43E;&#x43F;&#x4 +30;&#x440;&#x43A;<br /> &#x43E; &#x438; &#x43E; &#x43E; &#x435; <br /> &#x43A;&#x43E;&#x43C;&#x430;&#x440; &#x440; &#x439; <br /> &#x43B; &#x431; &#x442;&#x440;&#x430;&#x441;&#x441;&#x430;<br /> &#x451; &#x43A; &#x442; &#x431;<br /> &#x43F;&#x430;&#x441;&#x441; &#x443;&#x445;&#x43E;<br /> &#x43C;&#x435;&#x43D;&#x435;&#x434;&#x436;&#x435;&#x440;<br /> &#x444;&#x430;&#x443;&#x43D;&#x430; &#x440;&#x430;&#x43A;&#x438;<br /> &#x442;&#x43E;&#x441;&#x43A;&#x430; &#x433;<br /> &#x448;&#x43A;&#x430;&#x43D;&#x442; &#x43C; &#x435;<br /> &#x430;&#x442;&#x430;&#x43C;&#x430;&#x43D;<br /> &#x432; &#x442; &#x442; <br /> &#x43D; &#x443;&#x445;&#x430;&#x431;<br /> &#x438;&#x433;&#x440;&#x43E;&#x43A;&#x438;<br /> &#x43A; &#x433;&#x440;&#x430;&#x447;<br /></div></div> <!-- end # +outer-->

The spaces aren't represented as any special html entity. Do they need to be?

Another thing I want to rewrite about this routine is:

# surround by divs my $oitop = $vars{"oitop"}; my $oben = $oitop->slurp_utf8; my $oibottom = $vars{"oibottom"}; my $unten = $oibottom->slurp_utf8; my $text = $oben . $temp . $unten;

All this does is wrap a string with two other strings I keep in template files:

$ cat oi*.txt <div class="outer"> </div> <!-- end #outer--> $

Isn't there an xml way to do this?

I won't want to dwell on it forever, but I will show the relevant .css in the original post:

.outer { background-color:white; margin-left : auto; margin-right : auto; border-radius : 1em; clear: left; margin-top: .7em; margin-bottom: .7em; max-width:25%; display:block; } .hft-lines { background-color:white; margin-left : auto; margin-right : auto; border-radius : 1em; clear: left; margin-top: .7em; margin-bottom: .7em; max-width:85%; display:block; text-align:center; font-size: 1.5em; }

I am by no means a .css guru, so there might be low-hanging fruit for improvements. The page does register as without .css error.

So let me review and try to ask a cohesive question. As the script runs, I have a perfectly rectangular representation of these data as indicated by Data::Dump. The question is how to get this row and column nature to show up properly. I would imagine that there's more than one way to do it.

And remember, if you have cross words, do crosswords instead....

Thanks all for comments,

Replies are listed 'Best First'.
Re: getting html characters to align
by Your Mother (Archbishop) on Mar 07, 2019 at 03:43 UTC

    An option; probably too heavy on the idiom but that’s why it’s fun; for me. :P There are a few others with modern CSS. This is fragmentary HTML—it should be loaded properly in a full page—but it's just for example and modern browsers don't care. <td></td> has caveats in older browsers and maybe IE still for all I know but I will never launch that without being paid. You can put a &nbsp; in it if necessary but using whitespace to control HTML layout is painful to even suggest. :P

    #!/usr/bin/env perl
    use 5.16.0;
    use utf8;
    use strictures;
    use open ":std", ":encoding(utf8)";
    
    print <<"";
    <style>
    table {
      border:0;
      border-collapse:collapse;
    }
    td {
      font-weight:700;
      text-align:center;
      line-height:200%;
      font-size: 150%;
      border:2px solid black;
      width:2em;
      height:2em
    }
    td.fill {
      background-color:black;
    }
    </style>
    
    chomp( my @rows = <DATA> );
    
    say "<table>";
    for my $row ( @rows )
    {
        say "  <tr>";
        for my $col ( split //, $row )
        {
            say $col =~ /\w/ ?
                "    <td>$col</td>"
                :
                "    <td class='fill'></td>";
        }
        say "  </tr>";
    }
    say "</table>";
    
    __DATA__
    л лесопарк
    о и о о е
    комар р й
    л б трасса
    ё  к   т б
    пасс   ухо
      менеджер
    фауна раки
      тоска  г
    шкант м  е
        атаман
        в т т
        н ухаб
        игроки
        к грач
      using whitespace to control HTML layout is painful to even suggest

      Alright, I'm glad that's settled. Screenshot of output shows initial output. I'm really impressed with the style, thank you for writing it. The campaign continues between readmore tags:

      Thanks for your comments,

        #print $fh $$string; # ^^ print $fh $string;
        poj

        It seems like you are only missing <meta charset="UTF-8"> in the <head> part of your HTML. That is, take the HTML you currently generate, place it in <body>...</body> tags, then buryplace the body between <html>...</html>. Finally, add <head><meta charset="UTF-8"></head> before the body (but still inside html).

        Browsers tend to assume latin1 encoding when it hasn't been explicitly declared, so what you see is the result of decoding UTF-8-encoded text as Latin-1. Pages of your website have this tag, so they get decoded properly.

Re: getting html characters to align
by tangent (Parson) on Mar 07, 2019 at 02:44 UTC
    HTML does collapse whitespace, so if you have three spaces they will only display as one space. A common way to deal with that is to replace each space with the HTML entity &nbsp;. Another way to preserve whitespace is to add white-space: pre; to your .hft-lines CSS definition. The problem with both of these is that each letter is not the same width so the columns will still not line up.

    Another way would be to wrap your text in a <pre>..</pre> tag - this will preserve the whitespace and also the column widths, but it does that by using a mono-spaced font (one where each letter is given the same space), but this is usually not that pretty.

    I think your best solution would be to use a table, where each letter, and space, is put in a single cell. A bit cumbersome but you will then be able to keep the nice font.

    These modules both work with array of array structure:
    Text::Table::Tiny
    Text::Table::HTML

Re: getting html characters to align
by holli (Abbot) on Mar 07, 2019 at 09:35 UTC
    You don't need to be CSS guru to use a grid.

    CSS Grid Layout (aka "Grid"), is a two-dimensional grid-based layout system that aims to do nothing less than completely change the way we design grid-based user interfaces. CSS has always been used to lay out our web pages, but it's never done a very good job of it. First, we used tables, then floats, positioning and inline-block, but all of these methods were essentially hacks and left out a lot of important functionality (vertical centering, for instance)...


    holli

    You can lead your users to water, but alas, you cannot drown them.