Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 77602f2

Browse files
committed
Add support for \ulink and hyperlink grammars.
do_cmd_verbatiminput(): Write out a text file containing the content of the input file with a .txt extension, and add a link to it at the bottom of the presentation. This easier retrieval of example source code for copy & paste use.
1 parent bde8d1c commit 77602f2

1 file changed

Lines changed: 151 additions & 2 deletions

File tree

Doc/perl/python.perl

Lines changed: 151 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ sub do_cmd_let{
9090
sub do_cmd_textbar{ '|' . @_[0]; }
9191
sub do_cmd_infinity{ '∞' . @_[0]; }
9292
sub do_cmd_plusminus{ '±' . @_[0]; }
93+
sub do_cmd_menuselection{ @_[0]; }
94+
sub do_cmd_sub{ ' > ' . @_[0]; }
9395

9496

9597
# words typeset in a special way (not in HTML though)
@@ -324,6 +326,13 @@ sub do_cmd_rfc{
324326
. "$icon</a>" . $_);
325327
}
326328

329+
sub do_cmd_ulink{
330+
local($_) = @_;
331+
my $text = next_argument();
332+
my $url = next_argument();
333+
return "<a class=\"ulink\" href=\"$url\"\n >$text</a>" . $_;
334+
}
335+
327336
sub do_cmd_citetitle{
328337
local($_) = @_;
329338
my $url = next_optional_argument();
@@ -682,6 +691,135 @@ sub make_str_index_entry{
682691
return "$aname$str</a>";
683692
}
684693

694+
695+
%TokenToTargetMapping = ();
696+
%DefinedGrammars = ();
697+
%BackpatchGrammarFiles = ();
698+
699+
sub do_cmd_token{
700+
local($_) = @_;
701+
my $token = next_argument();
702+
my $target = $TokenToTargetMapping{"$CURRENT_GRAMMAR:$token"};
703+
if ($token eq $CURRENT_TOKEN || $CURRENT_GRAMMAR eq '*') {
704+
# recursive definition or display-only productionlist
705+
return "$token";
706+
}
707+
if ($target eq '') {
708+
$target = "<pyGrammarToken><$CURRENT_GRAMMAR><$token>";
709+
if (! $BackpatchGrammarFiles{"$CURRENT_FILE"}) {
710+
print "Adding '$CURRENT_FILE' to back-patch list.\n";
711+
}
712+
$BackpatchGrammarFiles{"$CURRENT_FILE"} = 1;
713+
}
714+
return "<a href=\"$target\">$token</a>" . $_;
715+
}
716+
717+
sub do_env_productionlist{
718+
local($_) = @_;
719+
my $lang = next_optional_argument();
720+
my $filename = "grammar-$lang.txt";
721+
if ($lang eq '') {
722+
$filename = 'grammar.txt';
723+
}
724+
local($CURRENT_GRAMMAR) = $lang;
725+
$DefinedGrammars{$lang} .= $_;
726+
return ("<dl><dd class=\"grammar\">\n"
727+
. "<div class=\"productions\">\n"
728+
. "<table cellpadding=\"2\" valign=\"baseline\">\n"
729+
. translate_commands(translate_environments($_))
730+
. "</table>\n"
731+
. "</div>\n"
732+
. (($lang eq '*')
733+
? ''
734+
: ("<a class=\"grammar-footer\"\n"
735+
. " href=\"$filename\" type=\"text/plain\"\n"
736+
. " >Download entire grammar as text.</a>\n"))
737+
. "</dd></dl>");
738+
}
739+
740+
sub do_cmd_production{
741+
local($_) = @_;
742+
my $token = next_argument();
743+
my $defn = next_argument();
744+
my $lang = $CURRENT_GRAMMAR;
745+
local($CURRENT_TOKEN) = $token;
746+
if ($lang eq '*') {
747+
return ("<tr>\n"
748+
. " <td><code>$token</code></td>\n"
749+
. " <td>&nbsp;::=&nbsp;</td>\n"
750+
. " <td><code>"
751+
. translate_commands($defn)
752+
. "</code></td></tr>"
753+
. $_);
754+
}
755+
my $target;
756+
if ($lang eq '') {
757+
$target = "$CURRENT_FILE\#tok-$token";
758+
}
759+
else {
760+
$target = "$CURRENT_FILE\#tok-$lang-$token";
761+
}
762+
$TokenToTargetMapping{"$CURRENT_GRAMMAR:$token"} = $target;
763+
return ("<tr>\n"
764+
. " <td><code><a name=\"tok-$token\">$token</a></code></td>\n"
765+
. " <td>&nbsp;::=&nbsp;</td>\n"
766+
. " <td><code>"
767+
. translate_commands($defn)
768+
. "</code></td></tr>"
769+
. $_);
770+
}
771+
772+
sub process_grammar_files{
773+
my $lang;
774+
my $filename;
775+
local($_);
776+
print "process_grammar_files()\n";
777+
foreach $lang (keys %DefinedGrammars) {
778+
$filename = "grammar-$lang.txt";
779+
if ($lang eq '*') {
780+
next;
781+
}
782+
if ($lang eq '') {
783+
$filename = 'grammar.txt';
784+
}
785+
open(GRAMMAR, ">$filename") || die "\n$!\n";
786+
print GRAMMAR strip_grammar_markup($DefinedGrammars{$lang});
787+
close(GRAMMAR);
788+
print "Wrote grammar file $filename\n";
789+
}
790+
my $PATTERN = '<pyGrammarToken><([^>]*)><([^>]*)>';
791+
foreach $filename (keys %BackpatchGrammarFiles) {
792+
print "\nBack-patching grammar links in $filename\n";
793+
my $buffer;
794+
open(GRAMMAR, "<$filename") || die "\n$!\n";
795+
# read all of the file into the buffer
796+
sysread(GRAMMAR, $buffer, 1024*1024);
797+
close(GRAMMAR);
798+
while ($buffer =~ /$PATTERN/) {
799+
my($lang, $token) = ($1, $2);
800+
my $target = $TokenToTargetMapping{"$lang:$token"};
801+
my $source = "<pyGrammarToken><$lang><$token>";
802+
$buffer =~ s/$source/$target/g;
803+
}
804+
open(GRAMMAR, ">$filename") || die "\n$!\n";
805+
print GRAMMAR $buffer;
806+
close(GRAMMAR);
807+
}
808+
}
809+
810+
sub strip_grammar_markup{
811+
local($_) = @_;
812+
s/\\production(<<\d+>>)(.+)\1/\n\2 ::= /g;
813+
s/\\token(<<\d+>>)(.+)\1/\2/g;
814+
s/\\e([^a-zA-Z])/\\\1/g;
815+
s/<<\d+>>//g;
816+
s/;SPMgt;/>/g;
817+
s/;SPMlt;/</g;
818+
s/;SPMquot;/\"/g;
819+
return $_;
820+
}
821+
822+
685823
$REFCOUNTS_LOADED = 0;
686824

687825
sub load_refcounts{
@@ -1490,6 +1628,7 @@ sub process_localmoduletables_in_file{
14901628
}
14911629
sub process_python_state{
14921630
process_all_localmoduletables();
1631+
process_grammar_files();
14931632
}
14941633

14951634

@@ -1685,11 +1824,18 @@ sub do_cmd_verbatiminput{
16851824
$file = "$texpath$dd$fname";
16861825
last if ($found = (-f $file));
16871826
}
1827+
my $srcname;
16881828
my $text;
16891829
if ($found) {
16901830
open(MYFILE, "<$file") || die "\n$!\n";
16911831
read(MYFILE, $text, 1024*1024);
16921832
close(MYFILE);
1833+
use File::Basename;
1834+
my $srcdir, $srcext;
1835+
($srcname, $srcdir, $srcext) = fileparse($file, '\..*');
1836+
open(MYFILE, ">$srcname.txt");
1837+
print MYFILE $text;
1838+
close(MYFILE);
16931839
#
16941840
# These rewrites convert the raw text to something that will
16951841
# be properly visible as HTML and also will pass through the
@@ -1715,9 +1861,12 @@ sub do_cmd_verbatiminput{
17151861
else {
17161862
$text = '<b>Could not locate requested file <i>$fname</i>!</b>\n';
17171863
}
1718-
return ($alltt_start
1864+
return ('<dl><dd><pre class="verbatim">'
17191865
. $text
1720-
. $alltt_end
1866+
. "</pre>\n<div class=\"verbatiminput-footer\">\n"
1867+
. "<a href=\"$srcname.txt\" type=\"text/plain\""
1868+
. ">Download as text.</a>"
1869+
. "\n</div>\n</dd></dl>"
17211870
. $_);
17221871
}
17231872

0 commit comments

Comments
 (0)