Extract xmlstarlet calls
This commit is contained in:
parent
fa66f118be
commit
5016ced432
2 changed files with 24 additions and 17 deletions
4
feed.rss
4
feed.rss
|
|
@ -6,11 +6,11 @@
|
||||||
<atom:link href="https://blog.jutty.dev/feed.rss" rel="self" type="application/rss+xml"/>
|
<atom:link href="https://blog.jutty.dev/feed.rss" rel="self" type="application/rss+xml"/>
|
||||||
<description>jutty.dev: Blog</description>
|
<description>jutty.dev: Blog</description>
|
||||||
<language>pt-br</language>
|
<language>pt-br</language>
|
||||||
<lastBuildDate>Sun, 19 May 2024 19:13:50 -0300</lastBuildDate>
|
<lastBuildDate>Sun, 19 May 2024 21:11:05 -0300</lastBuildDate>
|
||||||
<docs>https://www.rssboard.org/rss-specification</docs>
|
<docs>https://www.rssboard.org/rss-specification</docs>
|
||||||
<item>
|
<item>
|
||||||
<title>Scripts em OCaml</title>
|
<title>Scripts em OCaml</title>
|
||||||
<pubDate>Sun, 19 May 2024 19:13:50 -0300</pubDate>
|
<pubDate>Fri, 23 Feb 2024 21:23:00 -0300</pubDate>
|
||||||
<link>https://blog.jutty.dev/posts/scripts-em-ocaml.html</link>
|
<link>https://blog.jutty.dev/posts/scripts-em-ocaml.html</link>
|
||||||
<guid>https://blog.jutty.dev/posts/scripts-em-ocaml.html</guid>
|
<guid>https://blog.jutty.dev/posts/scripts-em-ocaml.html</guid>
|
||||||
<author>juno@jutty.dev (Juno Takano)</author>
|
<author>juno@jutty.dev (Juno Takano)</author>
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,21 @@
|
||||||
#!/usr/bin/env zsh
|
#!/usr/bin/env zsh
|
||||||
# dependencies: zsh, xmlstarlet, xmllint, jq
|
# dependencies: zsh, xmlstarlet, xmllint
|
||||||
|
|
||||||
alias xml="xmlstarlet"
|
|
||||||
date="$(date +'%a, %d %b %Y %H:%M:%S %z')"
|
date="$(date +'%a, %d %b %Y %H:%M:%S %z')"
|
||||||
|
|
||||||
|
function subnode {
|
||||||
|
local -r tag="${1}"
|
||||||
|
local -r value="${2}"
|
||||||
|
local path="${3:-rss/channel/new}"
|
||||||
|
local -r file="${4:-feed2.rss}"
|
||||||
|
|
||||||
|
if [[ -n $value ]]; then
|
||||||
|
/usr/bin/xmlstarlet ed -L --subnode "$path" -t elem -n "$tag" -v "$value" "$file"
|
||||||
|
else
|
||||||
|
/usr/bin/xmlstarlet ed -L --subnode "$path" -t elem -n "$tag" "$file"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
function insert {
|
function insert {
|
||||||
local -r title="$1"
|
local -r title="$1"
|
||||||
local -r slug="${2}.html"
|
local -r slug="${2}.html"
|
||||||
|
|
@ -14,14 +26,14 @@ function insert {
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
xml ed --update rss/channel/lastBuildDate --value "$date" feed.rss > feed2.rss
|
xmlstarlet ed --update rss/channel/lastBuildDate --value "$date" feed.rss > feed2.rss
|
||||||
|
|
||||||
xml ed -L --subnode rss/channel -t elem -n new feed2.rss
|
subnode new '' rss/channel
|
||||||
xml ed -L --subnode rss/channel/new -t elem -n title -v "$title" feed2.rss
|
subnode title "$title"
|
||||||
xml ed -L --subnode rss/channel/new -t elem -n pubDate -v "$date" feed2.rss
|
subnode pubDate "$date"
|
||||||
xml ed -L --subnode rss/channel/new -t elem -n link -v "$url_root/$slug" feed2.rss
|
subnode link "$url_root/$slug"
|
||||||
xml ed -L --subnode rss/channel/new -t elem -n guid -v "$url_root/$slug" feed2.rss
|
subnode guid "$url_root/$slug"
|
||||||
xml ed -L --subnode rss/channel/new -t elem -n author -v "$author" feed2.rss
|
subnode author "$author"
|
||||||
|
|
||||||
sed -i 's/<new>/<item>/g' feed2.rss
|
sed -i 's/<new>/<item>/g' feed2.rss
|
||||||
sed -i 's/<\/new>/<\/item>/g' feed2.rss
|
sed -i 's/<\/new>/<\/item>/g' feed2.rss
|
||||||
|
|
@ -30,6 +42,7 @@ function insert {
|
||||||
mv -v feed2.rss feed.rss
|
mv -v feed2.rss feed.rss
|
||||||
else
|
else
|
||||||
echo 'Generated an invalid feed, not overwriting'
|
echo 'Generated an invalid feed, not overwriting'
|
||||||
|
rm -vf feed2.rss
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
@ -40,15 +53,9 @@ total=$(echo $index | wc -l)
|
||||||
|
|
||||||
current=2
|
current=2
|
||||||
while [[ $current -le $total ]]; do
|
while [[ $current -le $total ]]; do
|
||||||
echo processing $current
|
|
||||||
lines=$(echo $index | tail -$((current)) | head -2)
|
lines=$(echo $index | tail -$((current)) | head -2)
|
||||||
title=$(echo $lines | grep title: | cut -d: -f 2 | xargs)
|
title=$(echo $lines | grep title: | cut -d: -f 2 | xargs)
|
||||||
slug=$(echo $lines | grep slug: | cut -d: -f 2 | xargs)
|
slug=$(echo $lines | grep slug: | cut -d: -f 2 | xargs)
|
||||||
echo title: $title
|
|
||||||
echo slug: $slug
|
|
||||||
insert "$title" "$slug"
|
insert "$title" "$slug"
|
||||||
((current+=2))
|
((current+=2))
|
||||||
done
|
done
|
||||||
|
|
||||||
echo -e "\nfeed.rss:"
|
|
||||||
cat feed.rss | xq -x
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue