-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhtmlreplace.pl
More file actions
21 lines (19 loc) · 915 Bytes
/
htmlreplace.pl
File metadata and controls
21 lines (19 loc) · 915 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
use strict;
use warnings;
local $/; # slurp whole file
while (<>) {
s/Retrieved from//g;
s|<p class="author" style="margin: 0.5em 0 -0.5em 0;">|<p class="author">|g;
# Add affiliate tag to Amazon URLs inside <a> tags
s{(?<!<a href=['"])https://amazon.com/dp/[0-9BCLXx-]{10,20}}{<a href='$&?tag=otexts20'>[Amazon]</a>}g;
# Process only inside csl-entry divs
s{(<div\b[^>]*\bclass="csl-entry"[^>]*>)(.*?)(</div>)}{
my ($open, $inner, $close) = ($1, $2, $3);
# Replace external anchors with bare anchors
$inner =~ s|<a\b[^>]*\bhref=([\"'])(https?://[^\"'<>\s]+)\1[^>]*>.*?</a>|<a href="$2"><i class="fa-solid fa-up-right-from-square"></i></a>|gi;
# Strip text from doc-biblioref anchors
$inner =~ s|<a\b([^>]*\brole="doc-biblioref"[^>]*)>.*?</a>|<a$1><i class="fa-solid fa-up-right-from-square"></i></a>|gi;
"$open$inner$close"
}gse;
print;
}