diff --git a/assets/js/data.js b/assets/js/data.js index 9894f2b..5178573 100644 --- a/assets/js/data.js +++ b/assets/js/data.js @@ -1,23 +1,22 @@ function get_data() { return { - pt: { - posts: [ + "pt": { + "posts": [ { - title: "Scripts em OCaml", - slug: "scripts-em-ocaml", - date: "23/03/2024", - }, + "title": "Scripts em OCaml", + "slug": "scripts-em-ocaml", + "date": "23/03/2024" + } ] }, - en: { - posts: [ + "en": { + "posts": [ { - title: "Hello, Worldling", - slug: "hello-worldling", - date: "May 20, 2024", - }, + "title": "Hello, Worldling", + "slug": "hello-worldling", + "date": "May 20, 2024" + } ] } } } - diff --git a/assets/js/main.js b/assets/js/main.js index 1f82a62..520e073 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -1,11 +1,12 @@ const data = get_data() -const post_list = document.getElementById('posts') const userLang = navigator.language || navigator.userLanguage let lang = userLang.includes('pt') ? 'pt' : 'en' -document.addEventListener('DOMContentLoaded', function() { +function populate_posts() { data[lang].posts.forEach(post => { + const posts = document.getElementById('nav-posts') + posts.innerHTML = '' const item = document.createElement('li') const anchor = document.createElement('a') const date = document.createElement('span') @@ -14,9 +15,9 @@ document.addEventListener('DOMContentLoaded', function() { date.innerText = `${parse_date(post.date)} - ` item.appendChild(date) item.appendChild(anchor) - post_list.appendChild(item) + posts.appendChild(item) }) -}) +} function parse_date(date) { current_year = new Date().getFullYear() @@ -24,3 +25,35 @@ function parse_date(date) { .replace(`/${current_year}`, '') } +function localize_menu() { + const home = document.getElementById('nav-menu-home') + const rss = document.getElementById('nav-menu-rss') + + if (lang == 'pt') { + home.innerText='Início' + rss.setAttribute('href', 'pt.rss') + } else { + home.innerText='Home' + rss.setAttribute('href', 'en.rss') + } +} + +const en_lang_swapper = document.getElementById('lang-swap-en') +const pt_lang_swapper = document.getElementById('lang-swap-pt') + +en_lang_swapper.addEventListener("click", () => { + lang = 'en' + populate_posts() + localize_menu() +}) + +pt_lang_swapper.addEventListener("click", () => { + lang = 'pt' + populate_posts() + localize_menu() +}) + +document.addEventListener('DOMContentLoaded', () => { + populate_posts() + localize_menu() +}) diff --git a/assets/style.css b/assets/style.css index 9f3637b..e1c8af1 100644 --- a/assets/style.css +++ b/assets/style.css @@ -15,6 +15,11 @@ html { color: #888 } +#nav-menu li { + display: inline-block; + margin-left: 20px; +} + p { margin: 30px; } @@ -46,8 +51,8 @@ h2 { margin: 20px 0px 10px 20px; } -ul { - margin-top: 10px; +#nav-posts { + margin-top: 40px; color: #F1E9E5; } diff --git a/en.rss b/en.rss new file mode 100644 index 0000000..57cdcff --- /dev/null +++ b/en.rss @@ -0,0 +1,19 @@ + + + + jutty.dev: Blog + http://blog.jutty.dev/ + + jutty.dev: Blog + en-us + Mon, 20 May 2024 12:06:59 -0300 + https://www.rssboard.org/rss-specification + + Hello, Worldling + Mon, 20 May 2024 12:06:59 -0300 + https://blog.jutty.dev/posts/hello-worldling.html + https://blog.jutty.dev/posts/hello-worldling.html + juno@jutty.dev (Juno Takano) + + + diff --git a/index.html b/index.html index 72fc26a..f65262e 100644 --- a/index.html +++ b/index.html @@ -5,7 +5,8 @@ jutty.dev - + + @@ -28,7 +29,14 @@ diff --git a/posts/hello-worldling.html b/posts/hello-worldling.html index 12648fb..5adecf0 100644 --- a/posts/hello-worldling.html +++ b/posts/hello-worldling.html @@ -17,7 +17,8 @@ .display.math{display: block; text-align: center; margin: 0.5rem auto;} - + + diff --git a/posts/scripts-em-ocaml.html b/posts/scripts-em-ocaml.html index 61af01c..1b795e6 100644 --- a/posts/scripts-em-ocaml.html +++ b/posts/scripts-em-ocaml.html @@ -78,7 +78,8 @@ .display.math{display: block; text-align: center; margin: 0.5rem auto;} - + + diff --git a/posts/template.html b/posts/template.html index 780c2da..9bbd0bf 100644 --- a/posts/template.html +++ b/posts/template.html @@ -23,7 +23,8 @@ $endif$ $for(css)$ $endfor$ - + + $for(header-includes)$ $header-includes$ diff --git a/feed.rss b/pt.rss similarity index 92% rename from feed.rss rename to pt.rss index 2fce9e6..68d5ce4 100644 --- a/feed.rss +++ b/pt.rss @@ -6,7 +6,7 @@ jutty.dev: Blog pt-br - Sun, 19 May 2024 21:11:05 -0300 + Mon, 20 May 2024 12:06:59 -0300 https://www.rssboard.org/rss-specification Scripts em OCaml diff --git a/update-feed.zsh b/update-feed.zsh index 57ffe2e..d34f6e7 100755 --- a/update-feed.zsh +++ b/update-feed.zsh @@ -2,12 +2,13 @@ # dependencies: zsh, xmlstarlet, xmllint date="$(date +'%a, %d %b %Y %H:%M:%S %z')" +data='assets/js/data.js' function subnode { local -r tag="${1}" local -r value="${2}" local path="${3:-rss/channel/new}" - local -r file="${4:-feed2.rss}" + local -r file="${4:-tmp.rss}" if [[ -n $value ]]; then /usr/bin/xmlstarlet ed -L --subnode "$path" -t elem -n "$tag" -v "$value" "$file" @@ -19,14 +20,15 @@ function subnode { function insert { local -r title="$1" local -r slug="${2}.html" + local -r file="${3}" local -r url_root='https://blog.jutty.dev/posts' local -r author='juno@jutty.dev (Juno Takano)' - if grep -q "$url_root/$slug" feed.rss; then + if grep -q "$url_root/$slug" "$file"; then return 0 fi - xmlstarlet ed --update rss/channel/lastBuildDate --value "$date" feed.rss > feed2.rss + xmlstarlet ed --update rss/channel/lastBuildDate --value "$date" "$file" > tmp.rss subnode new '' rss/channel subnode title "$title" @@ -35,27 +37,50 @@ function insert { subnode guid "$url_root/$slug" subnode author "$author" - sed -i 's///g' feed2.rss - sed -i 's/<\/new>/<\/item>/g' feed2.rss + sed -i 's///g' tmp.rss + sed -i 's/<\/new>/<\/item>/g' tmp.rss - if xmllint --noout feed2.rss; then - mv -v feed2.rss feed.rss + if xmllint --noout tmp.rss; then + mv -v tmp.rss "$file" else echo 'Generated an invalid feed, not overwriting' - rm -vf feed2.rss + rm -v tmp.rss return 1 fi } -index="$(cat assets/js/data.js | grep 'title\|slug' | sed 's/,/\n/g' | grep '.')" -total=$(echo $index | wc -l) +function assemble_index { + local -r lang="$1" -current=2 -while [[ $current -le $total ]]; do - lines=$(echo $index | tail -$((current)) | head -2) - title=$(echo $lines | grep title: | cut -d: -f 2 | xargs) - slug=$(echo $lines | grep slug: | cut -d: -f 2 | xargs) - insert "$title" "$slug" - ((current+=2)) -done + cat "$data" | + grep -v function | + sed 's/ *return //' | + sed -E ':a;N;$!ba; s/(.*)\}(.*)/\1\2/' | + jq ".$lang" | + grep 'title\|slug' | + grep '.' +} + +index_en=$(assemble_index en) +index_pt=$(assemble_index pt) + +function insert_from { + local -r index="$1" + local -r file="$2" + local -r total=$(echo $index | wc -l) + local title + local slug + + current=2 + while [[ $current -le $total ]]; do + lines=$(echo $index | tail -$((current)) | head -2) + title=$(echo $lines | grep title | cut -d'"' -f 4 | xargs) + slug=$(echo $lines | grep slug | cut -d'"' -f 4 | xargs) + insert "$title" "$slug" "$file" + ((current+=2)) + done +} + +insert_from "$index_pt" pt.rss +insert_from "$index_en" en.rss