| 1 |
%% |
| 2 |
%% This is file `example.tex', |
| 3 |
%% generated with the docstrip utility. |
| 4 |
%% |
| 5 |
%% The original source files were: |
| 6 |
%% |
| 7 |
%% perltex.dtx (with options: `example') |
| 8 |
%% |
| 9 |
%% This is a generated file. |
| 10 |
%% |
| 11 |
%% Copyright (C) 2003-2024 Scott Pakin <[email protected]> |
| 12 |
%% |
| 13 |
%% This file may be distributed and/or modified under the conditions of |
| 14 |
%% the LaTeX Project Public License, either version 1.3c of this license |
| 15 |
%% or (at your option) any later version. The latest version of this |
| 16 |
%% license is in: |
| 17 |
%% |
| 18 |
%% http://www.latex-project.org/lppl.txt |
| 19 |
%% |
| 20 |
%% and version 1.3c or later is part of all distributions of LaTeX version |
| 21 |
%% 2006/05/20 or later. |
| 22 |
%% |
| 23 |
\documentclass{article} |
| 24 |
\usepackage[optional]{perltex} |
| 25 |
\usepackage{environ} |
| 26 |
\usepackage{hyperref} |
| 27 |
|
| 28 |
\ifperl |
| 29 |
|
| 30 |
\perlnewcommand{\dolinkwords}[3]{ |
| 31 |
# Preprocess our arguments. |
| 32 |
$url = $_[0]; |
| 33 |
$url =~ s/\\\%s/\%s/g; |
| 34 |
%stopwords = map {lc $_ => 1} split " ", $_[1]; |
| 35 |
$stopwords{""} = 1; |
| 36 |
$text = $_[2]; |
| 37 |
|
| 38 |
# Replace LaTeX code in the text with placeholders. |
| 39 |
$placeholder = "ABCxyz123"; |
| 40 |
%substs = (); |
| 41 |
$replace = sub {$substs{$placeholder} = $_[0]; $placeholder++}; |
| 42 |
$text =~ s/\\(begin|end)\s+\{[a-z]+\}/$replace->($&)/gse; |
| 43 |
$text =~ s/\\[a-z]+/$replace->($&)/gse; |
| 44 |
|
| 45 |
# Hyperlink each word that's not in the stop list. |
| 46 |
$newtext = ""; |
| 47 |
foreach $word (split /((?<=[-\A\s])[\'a-z]+\b)/i, $text) { |
| 48 |
$lcword = lc $word; |
| 49 |
if (defined $stopwords{$lcword} || $lcword =~ /[^a-z]/) { |
| 50 |
$newtext .= $word; |
| 51 |
} |
| 52 |
else { |
| 53 |
$newtext .= sprintf "\\href{$url}{%s}", $lcword, $word; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
# Restore original text from placeholders and return the new text. |
| 58 |
while (($tag, $orig) = each %substs) { |
| 59 |
$newtext =~ s/\Q$tag\E/$orig/gs; |
| 60 |
} |
| 61 |
return $newtext; |
| 62 |
} |
| 63 |
|
| 64 |
\NewEnviron{linkwords}[2][]{\dolinkwords{#2}{#1}{\BODY}}{} |
| 65 |
|
| 66 |
\else |
| 67 |
|
| 68 |
\newenvironment{linkwords}[2][]{}{} |
| 69 |
|
| 70 |
\fi |
| 71 |
|
| 72 |
\begin{document} |
| 73 |
|
| 74 |
\newcommand{\stopwords}{a an the of in am and or but i we me you us them} |
| 75 |
|
| 76 |
\begin{linkwords}[\stopwords]{http://www.google.com/search?q=define:\%s} |
| 77 |
\begin{verse} |
| 78 |
I'm very good at integral and differential calculus; \\ |
| 79 |
I know the scientific names of beings animalculous: \\ |
| 80 |
In short, in matters vegetable, animal, and mineral, \\ |
| 81 |
I am the very model of a modern Major-General. |
| 82 |
\end{verse} |
| 83 |
\end{linkwords} |
| 84 |
|
| 85 |
\end{document} |
| 86 |
\endinput |
| 87 |
%% |
| 88 |
%% End of file `example.tex'. |