[l2h] Re: Latex2HTML and Prettyref

Jon Schutz Jon.Schutz@youramigo.com
Thu, 31 Jan 2002 23:05:30 +1030


--4c0BeXnIwT
Content-Type: text/plain; charset=us-ascii
Content-Description: message body and .signature
Content-Transfer-Encoding: 7bit


Ross,

Please find attached my implementation of prettyref.perl.  It works
very well for me, although I'm sure it won't handle all possible
cases.  Please feel free to redistribute and/or modify it, or not, as
you wish.

Thanks again for your help.

-- 
Jon Schutz			      Email: Jon.Schutz@YourAmigo.com
				         Ph: +61 (0)8 8211 9211
Chief Technology Officer	        Fax: +61 (0)8 8211 6356
YourAmigo Pty Ltd		        Mob: +61 (0)427 004 875




--4c0BeXnIwT
Content-Type: text/plain
Content-Disposition: inline;
	filename="prettyref.perl"
Content-Transfer-Encoding: 7bit

# This is taken from process_ref, with modifications to get the prettyref label inside the HREF.
sub process_prettyref {
    local($ref_mark, $id, $pre_label, $label, $post_label) = @_;
    $pre_label = &balance_inner_tags($pre_label) 
	if $pre_label =~ (/<\/([A-Z]+)>($math_verbatim_rx.*)<\1>/);
    $pre_label = &translate_environments($pre_label);
    $pre_label = &simplify(&translate_commands($pre_label))
	if ($pre_label =~ /\\/ );

    local($pretag) = &get_next_optional_argument;
    $pretag = &translate_commands($pretag) if ($pretag =~ /\\/);    

   # This borrowed from xspace.perl    
    local($space) = " ";
    $space = "" if /^([{}~.!,:;?\/'\)-]|\\\/|\\ )/;

    if ($label) {
	$label =~ s/<[^>]*>//go ; #RRM: Remove any HTML tags
	$label =~ s/$label_rx/_/g;	# replace non alphanumeric characters

#	if ($symbolic_labels{$pretag.$label}) {
	    $use_label = join('', $pre_label, $symbolic_labels{$pretag.$label}, $post_label);
#	}

	print "\nLINK: $ref_mark\#$label\#$id  :$use_label:" if ($VERBOSITY > 3);
	# The quotes around the HREF are inserted later
	join('',"<A HREF=$ref_mark#$label#$id>$use_label<\/A>", $space, $_);
    }
    else {
	print "Cannot find label argument after <$last_word>\n" if $last_word;
	$post_label . $_;
    }
}

sub do_cmd_prettyref {
   local($_) = @_;
   my($pre_label, $label, $post_label, $id)=('','','','');

   $label = &missing_braces unless (
	(s/$next_pair_pr_rx/($id, $label) = ($1, $2);''/eo)
	||(s/$next_pair_rx/($id, $label) = ($1, $2);''/eo));

  SWITCH: {
     if ($label =~ /^eq/) {
       $pre_label = "Eq ("; $post_label = ")"; last SWITCH;
     }
     if ($label =~ /^lem/) {
       $pre_label = "Lemma "; last SWITCH;
     }
     if ($label =~ /^thm/) {
       $pre_label = "Theorem "; last SWITCH;
     }
     if ($label =~ /^cha/) {
       $pre_label = "Chapter "; last SWITCH;
     }
     if ($label =~ /^sec/) {
       $pre_label = "Section "; last SWITCH;
     }
     if ($label =~ /^tab/) {
       $pre_label = "Table "; last SWITCH;
     }
     if ($label =~ /^fig/) {
       $pre_label = "Figure "; last SWITCH;
     }
     if ($label =~ /^app/) {
       $pre_label = "Appendix "; last SWITCH;
     }
   }
   process_prettyref($cross_ref_mark, $id, $pre_label, $label, $post_label);

}

1;

--4c0BeXnIwT
Content-Type: text/plain; charset=us-ascii
Content-Description: message body text
Content-Transfer-Encoding: 7bit



>>>>> "Ross" == Ross Moore <ross@ics.mq.edu.au> writes:

    >> Dear Ross,
    >> 
    >> I could do with a little bit of help.  I've tried to help
    >> myself by searching the latex2html mailling list archives, but
    >> I can't seem to find a working server for them at the moment.
    >> 
    >> I need an implementation of prettyref.sty for latex2html.  I
    >> have come up with a fairly feeble framework for prettyref.perl,
    >> which half works but does some things I don't understand.
    >> 
    >> If you could help by pointing me to some developer
    >> documentation, or to an existing implementation of
    >> prettyref.perl, or offering any advice which can get me further
    >> along, it would be most appreciated.
    >> 
    >> What I don't understand is the arg list passed to the
    >> do_cmd_prettyref function; I assumed I would be getting, in
    >> response to the latex code

    Ross> It doesn't pass a list of arguments, it passes (a pointer
    Ross> to) the full stream following the call to the TeX macro.
    Ross> The (implementation) of the macro must extract its own
    Ross> arguments, since TeX is capable of matching any pattern at
    Ross> all.  Even with LaTeX there's the possibility of having
    Ross> `optional' arguments, so you cannot assume a simple sequence
    Ross> #1 #2 #3, etc.


    >> \prettyref{sec:mysection}, do_cmd_prettyref(sec:mysection).  In
    >> my


    Ross> To see what happens with \prettyref{sec:mysection} run the
    Ross> job with -verbosity 9 .

    Ross> You will get log-messages showing:

    Ross> *** Translating commands
    Ross> ***\prettyref<#1#>sec:mysection<#1#> .prettyref IN:
    Ross> \prettyref<#1#>sec:mysection<#1#>

    Ross> So the call to &do_cmd_prettyprint will receive:

    Ross>   @_ <--- <#1#>sec:mysection<#1#>

    Ross> The pattern /^sec/ does not match.  You need to extract the
    Ross> argument first, then apply your test to the container with
    Ross> the extracted argument.


    Ross> The usual way of doing this is:

    Ross>     my($label); $label = &missing_braces unless (
    Ross> (s/$next_pair_pr_rx/$label =$2;''/eo)
    Ross> ||(s/$next_pair_rx/$label =$2;''/eo));

    Ross> which caters for 2 levels of processing, and gives a
    Ross> warning-message in case \prettyref is called without a
    Ross> braced argument.  (Note that successfully finding the
    Ross> argument removes it from $_.)

    Ross> Now you should apply your tests to $label, not to $_ .


    >> code below, I get "Unknown Prefix sec:mysection" as the output
    >> when run from latex2html, but "Section sec:mysection" if I
    >> invoke it directly.
 
 
    Ross> sub do_cmd_prettyref { local($_) = @_; my($text,
    Ross> $label)=('',''); $label = &missing_braces unless (
    Ross> (s/$next_pair_pr_rx/$label =$2;''/eo)
    Ross> ||(s/$next_pair_rx/$label =$2;''/eo)); local(@s) =
    Ross> split(/:/, $label);

    Ross>   SWITCH: { if ($label =~ /^fig/) { $text = "Figure"; last
    Ross> SWITCH; } if ($label =~ /^sec/) { $text = "Section"; last
    Ross> SWITCH; } if ($label =~ /^tab/) { $text = "Table"; last
    Ross> SWITCH; } $text = "Unknown Prefix";
 
    Ross>    }
 
    Ross>    join(' ', $text, $_); }

    Ross> This now should work correctly; viz.
    Ross> http://www-texdev.mpce.mq.edu.au/SCHUTZ/prtest/



--4c0BeXnIwT--