-
-
Notifications
You must be signed in to change notification settings - Fork 308
/
urlify.pl
47 lines (32 loc) · 1.18 KB
/
urlify.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use strict;
# Code to create section-ids from plain text is used both in mkindex and in uni.pl
sub urlify {
my ($section)=@_;
if($section =~ m/(.*)\[(.*)\]\(.*\).*/) {
# The section header contains a link to somewhere else, we'll strip out the "text" portion
# for use in the 'urlify' output
my $old_section = $section;
$section = "$1$2";
}
# convert letters to lower case
$section =~ tr/[A-Z]/[a-z]/;
# Convert all '<' to '-less-than'
$section =~ s/\</-less-than-/g;
# Convert all '>' to '-greater-than'
$section =~ s/\>/-greater-than-/g;
# Convert all '.' to '-dot'
$section =~ s/\./-dot-/g;
# Convert all '/' to '-slash'
$section =~ s/\//-slash-/g;
# remove rubbish
$section =~ s/[*`'":\(\),]+//g;
# convert anything left that isn't a dash, underscore, number or letter
$section =~ s/[^_a-zA-Z0-9-]/-/g;
# If the starting chars aren't a letter or underscore, prepend "sect-" to them to turn them
# into legal identifiers
$section =~ s/^([^_a-zA-Z]+)/sect-$1/g;
# strip trailing dash '-' characters from the section header
$section =~ s/-+$//;
return "$section";
}
1;