Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F33963611
extlnks.awk
No One
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Authored By
Green_Cardamom
Dec 21 2020, 8:26 PM
2020-12-21 20:26:28 (UTC+0)
Size
4 KB
Referenced Files
None
Subscribers
None
extlnks.awk
View Options
#!/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.ia"
)
if
(
length
(
iaout
)
==
0
)
{
iaout
=
readfile
(
id
"out.librivox"
)
if
(
length
(
iaout
)
==
0
)
{
print
0
exit
}
else
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
}
File Metadata
Details
Attached
Mime Type
text/plain; charset=utf-8
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
8811898
Default Alt Text
extlnks.awk (4 KB)
Attached To
Mode
P13620 extlnks.awk
Attached
Detach File
Event Timeline
Log In to Comment