Page MenuHomePhabricator
Paste P13620

extlnks.awk
ActivePublic

Authored by Green_Cardamom on Dec 21 2020, 8:08 PM.
Tags
None
Referenced Files
F33968139: extlnks.awk
Dec 22 2020, 3:57 PM
F33963611: extlnks.awk
Dec 21 2020, 8:26 PM
F33963594: extlnks.awk
Dec 21 2020, 8:08 PM
Subscribers
None
#!/bin/awk -f
@load "filefuncs"
@load "readfile"
BEGIN {
# ################################################################################################
#
# External Links
# Insert new External Links into an article at the right spot.
# User:GreenC November 2015
#
# Pass variable id:
#
# awk -v id="/home/greenc/wi-awb/temp/wi-awb-1102154815/" -f extlnks.awk
#
# Directory "id" should already contain two files:
#
# article.txt .. wikitext of the article
# out.librivox .. the template to be added
#
# Output file:
#
# article.extlnks.txt .. updated version with external link added
#
# ################################################################################################
iaout = readfile(id "out.librivox")
lv = 1
if(exists(id "out.extlnk")) { # No ==External links== header found. Add it.
location["last header"] = location_string(id, "==")
location["persondata"] = location_string(id, "[{]{2}[ ]{0,1}[Pp]ersondata")
location["authority"] = location_string(id, "[{]{2}[ ]{0,1}[Aa]uthority")
location["start"] = location_string(id, "[{]{2}[ ]{0,1}[Ss][-]start")
location["startbox"] = location_string(id, "[{]{2}[ ]{0,1}[Ss]tart [Bb]ox")
location["metadata"] = location_string(id, "<!--[ ]{0,2}[Mm]etadata: see")
location["default"] = location_string(id, "[{]{2}[ ]{0,1}[Dd][Ee][Ff][Aa][Uu][Ll][Tt][Ss][Oo][Rr][Tt]")
PROCINFO["sorted_in"] = "@val_num_asc"
point = 0
for(idx in location) {
if(idx ~ "last header" || location[idx] == 0)
continue
point = location[idx]
break
}
if(point != 0) {
insert_header(id, point, "==External links==")
print 1
exit
}
else {
print 0
exit
}
}
if(insert_link(id, iaout) == 1) { # 1 = successfully inserted the external link.
print 1
exit
} else {
print 0
exit
}
}
function location_string(id, str ,line,articleorig,i,re,loc) {
articleorig = id "article.txt"
i = loc = 0
re = "^" str
while ((getline line < articleorig) > 0) {
i++
if(match(line, re))
loc = i
}
close(articleorig)
return loc
}
function insert_header(id, point, str ,articleorig, articlenew,i,line) {
articleorig = id "article.txt"
articlenew = id "article.extlnks.txt"
i = 0
while ((getline line < articleorig) > 0) {
i++
if(i == point) {
printf("%s\n",str) >> articlenew
printf("%s\n",iaout) >> articlenew
printf("%s\n",line) >> articlenew
continue
}
printf("%s\n",line) >> articlenew
}
close(articlenew)
}
function insert_link(id,iaout, line,zone,articleorig,done) {
zone = done = 0
articleorig = id "article.txt"
while ((getline line < articleorig) > 0) {
if( (zone == 1 || zone == 2) && ! done) {
if(substr(line,1,1) == "*") {
if(zone == 1) {
printf("%s",iaout) >> id "article.extlnks.txt"
printf("%s\n",line) >> id "article.extlnks.txt"
}
else if(zone == 2) {
printf("%s\n",line) >> id "article.extlnks.txt"
printf("%s",iaout) >> id "article.extlnks.txt"
}
zone = 0
done = 1
continue
}
else if(substr(line,1,1) == "" && lv) {
printf("%s",iaout) >> id "article.extlnks.txt"
printf("%s\n",line) >> id "article.extlnks.txt"
zone = 0
done = 1
continue
}
else
zone = 0
}
else
zone = 0
if(! lv && line ~ /={2,3}[ ]{0,2}[Ee]xternal [Ll]inks[ ]{0,2}={2,3}/)
zone = 1
if(lv && line ~ /[*][ ]*[{][{][ ]*[Gg]utenberg author/)
zone = 2
if(lv && line ~ /[*][ ]*[{][{][ ]*[Ii]nternet [Aa]rchive author/)
zone = 1
printf("%s\n",line) >> id "article.extlnks.txt"
}
close(articleorig)
close(id "article.extlnks.txt")
return done
}
#
# Check for file existence. Return 1 if exists, 0 otherwise.
# Requires GNU Awk:
# @load "filefuncs"
#
function exists(name ,fd) {
if ( stat(name, fd) == -1)
return 0
else
return 1
}