scripts/markdown-hyperlinks: prevent unwanted replacements

- don't replace @var in code spans with link to github user
- don't replace 1234567... in links with link to commit
- replace hexadecimal numbers looking like commit hashes only
  after replacing raw URIs - this together with the previous point
  prevents inadvertent replacement of long numbers in links

[skip ci]
This commit is contained in:
Mickey Rose 2018-07-09 11:31:15 +02:00
parent e9befd9057
commit 70a511165b

View file

@ -17,9 +17,12 @@ my $repo = qr/ [a-zA-Z] [a-zA-Z0-9.-]* /x;
while (<>) {
# make links from @username references
# (except when escaped like \@foobar or `@foobar`)
s;(?<! \[ | \\ | \` ) \@ ($user) \b (?! \] )
;[\@$1](https://github.com/$1);xg;
# (except when escaped like \@foobar or in code like `where !@barman! = 'Moe'`)
s"(?: (`++) .*? \g{-1} (*SKIP) (*FAIL) )? # skip over code spans
(?<! \[ | \\ ) # no match after [ or \
\@ ($user) \b
(?! \] ) # no match before ]
"[\@$2](https://github.com/$2)"xg;
# make links from #1234 references (except when escaped like \#5)
# we can't tell whether the number refers to an issue or a pull request,
@ -27,11 +30,6 @@ while (<>) {
s;(?<! \[ | \\ ) \# ([0-9]+) \b (?! \] )
;[\#$1](https://github.com/mapnik/mapnik/issues/$1);xg;
# make links from commit hashes
# (accept 7 or 9-40 hex digits, but not 8 which could be a date)
s;(?<! \[ | / ) \b (([0-9a-f]{7})([0-9a-f]{2,33})?) \b (?! \] )
;[$2](https://github.com/mapnik/mapnik/commit/$1);xg;
# make shortcut links from raw URIs (which GFM turns into proper links,
# but doesn't contract even though it could)
# - issues
@ -45,5 +43,14 @@ while (<>) {
s;(?<! \] \( ) (https://github\.com/($user/$repo)/commit/([0-9a-f]{7})[0-9a-f]{0,33}) \b
;[$2\@$3]($1);xg;
# make links from commit hashes
# (accept 7 or 9-40 hex digits, but not 8 which could be a date)
s"(?: (`++) .*? \g{-1} (*SKIP) (*FAIL) )? # skip over code spans
(?: \[.*?\] \(.*?\) (*SKIP) (*FAIL) )? # skip over links
(?<! / ) # no match after /
\b (([0-9a-f]{7}) # 7 digits for link text
([0-9a-f]{2,33})?) \b # maybe 2-33 more digits
"[$3](https://github.com/mapnik/mapnik/commit/$2)"xg;
print;
}