<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
        <title>Лента комментариев</title>
        <link>https://evgenykuznetsov.org/code/static-webmentions/</link>
        <description>static-webmentions на DIMV - Лента комментариев</description>
        <generator>Hugo -- gohugo.io</generator><language>ru</language><managingEditor>evgeny@kuznetsov.md (Евгений Кузнецов)</managingEditor>
            <webMaster>evgeny@kuznetsov.md (Евгений Кузнецов)</webMaster>
            <atom:link href="https://evgenykuznetsov.org/code/static-webmentions/comments.xml" rel="self" type="application/rss+xml" />
        <atom:link href="https://switchboard.p3k.io/" rel="hub" />
    <atom:link href="https://evgenykuznetsov.superfeedr.com/" rel="hub" />
    <item>
    <title>Automating Links to POSSE Copies On a Static Website</title>
    <link>https://evgenykuznetsov.org/en/posts/2021/syndication/</link>
    <pubDate>Sun, 26 Dec 2021 13:26:09 &#43;0000</pubDate>
    <author>Evgeny Kuznetsov</author>
    <guid>https://evgenykuznetsov.org/en/posts/2021/syndication/_https://evgenykuznetsov.org/en/go/static-webmentions/_1322526</guid>
    <description><![CDATA[<p>Up until today, I’ve been adding the links to POSSE copies of my posts manually. It <a href="https://indieweb.org/manual_until_it_hurts">hurt</a> so much that I didn’t do it at all most of the time. I’ve been toying with the idea of automating this task for quite a while, but with a statically generated website it’s not very straightforward. I made it work, though.</p>
<p>My site is built with <a href="https://gohugo.io">Hugo</a>. Each time I make an update, my server fetches the new copy from the git server, runs Hugo to build the site, and then runs <a href="https://evgenykuznetsov.org/en/go/static-webmentions/">static-webmentions</a> to send the webmentions out. These usually include a webmention to <a href="https://brid.gy/about#publishing">Brid.gy</a> so that the page gets published on Twitter (or GitHub). Like many other IndieWeb syndication tools, Brid.gy’s webmention endpoint replies with a <code>201 Created</code> HTTP code, and the reply includes a <code>Location</code> HTTP header that holds the URL of the created page (a tweet).</p>
<p>Since version 0.7, static-webmentions looks for these <code>Location</code> headers and outputs a string like:</p>
<pre><code>created for https://evgenykuznetsov.org/reactions/2021/re-360073803/ is https://twitter.com/nekr0z/status/1475008644047675392
</code></pre><p>I can use <code>awk</code> to extract the page URL and the POSSE copy URL from this output, store it to a temporary file, and then feed them pair by pair to a <code>frontmatter.sh</code> script that would to the magic. This is what it looks like in the script that is used to build my site:</p>
<pre><code>static-webmentions <span>|</span> tee &gt;<span>(</span>awk <span>'/^created for/ {print $3, $NF}'</span> &gt; ~/posse_list<span>)</span>

    <span>[</span>...<span>]</span>

<span>while</span> <span>read</span> mypage newposse<span>;</span> <span>do</span>
    frontmatter.sh <span>$mypage</span> posse <span>$newposse</span>
<span>done</span> &lt; ~/posse_list
</code></pre><p>What is <code>frontmatter.sh</code> supposed to be doing? Well, every post of my site is stored in a Markdown file that has a little configuration “intro” at the beginning. These “intros” are called <em>Front Matter</em> in Hugo, and can be in various formats. I happen to like TOML best, so my Front Matters are all TOML. This means they start and end with <code>+++</code>, and also means that the links to POSSE copies that Hugo will correctly incorporate in my pages look something like this:</p>
<pre><code><span>posse</span> <span>=</span> <span>[</span><span>"https://twitter.com/my/tweet"</span><span>,</span> <span>"https://news.indieweb.org/this/article"</span><span>]</span>
</code></pre><p>That’s the job for the script: first, to locate the file responsible for the page, and second, to add the URL of the POSSE copy to that file’s front matter. Figuring out how to do it took me quite a while, and the current version of my <code>frontmatter.sh</code> looks like this:</p>



<pre><code><span> 1
</span><span> 2
</span><span> 3
</span><span> 4
</span><span> 5
</span><span> 6
</span><span> 7
</span><span> 8
</span><span> 9
</span><span>10
</span><span>11
</span><span>12
</span><span>13
</span><span>14
</span><span>15
</span><span>16
</span><span>17
</span><span>18
</span><span>19
</span><span>20
</span><span>21
</span><span>22
</span><span>23
</span><span>24
</span><span>25
</span><span>26
</span><span>27
</span><span>28
</span><span>29
</span><span>30
</span><span>31
</span><span>32
</span><span>33
</span><span>34
</span><span>35
</span><span>36
</span><span>37
</span><span>38
</span><span>39
</span><span>40
</span><span>41
</span><span>42
</span><span>43
</span><span>44
</span><span>45
</span><span>46
</span><span>47
</span><span>48
</span><span>49
</span><span>50
</span><span>51
</span><span>52
</span><span>53
</span><span>54
</span><span>55
</span><span>56
</span><span>57
</span><span>58
</span><span>59
</span><span>60
</span></code></pre>


<pre><code><span>#!/bin/bash
</span><span></span><span>set</span> -e

findfile<span>()</span> <span>{</span>
        <span>local</span> <span>__variable</span><span>=</span><span>$1</span>
        <span>local</span> <span>sitepath</span><span>=</span><span>`</span><span>echo</span> <span>$2</span> <span>|</span> sed <span>'s|^https://evgenykuznetsov.org/||g'</span><span>`</span>
        <span>local</span> <span>lang</span><span>=</span><span>"ru"</span>

        <span>[[</span> <span>"</span><span>$sitepath</span><span>"</span> <span>=</span>~ ^en <span>]]</span> <span>&amp;&amp;</span> <span>local</span> <span>lang</span><span>=</span><span>"en"</span> <span>&amp;&amp;</span> <span>local</span> <span>sitepath</span><span>=</span><span>`</span><span>echo</span> <span>"</span><span>$sitepath</span><span>"</span> <span>|</span> sed <span>'s|^en/||g'</span><span>`</span>

        <span>local</span> <span>path</span><span>=</span><span>`</span><span>echo</span> content/<span>"</span><span>$sitepath</span><span>"</span><span>`</span>
        <span>if</span> <span>[</span> -d <span>"</span><span>$path</span><span>"</span> <span>]</span><span>;</span> <span>then</span>
                <span>local</span> <span>path</span><span>=</span><span>"</span><span>$path</span><span>""index."</span>
        <span>else</span>
                <span>local</span> <span>path</span><span>=</span><span>${</span><span>path</span><span>%</span><span>"/"</span><span>}</span>
        <span>fi</span>

        <span>if</span> <span>[</span> <span>"</span><span>$lang</span><span>"</span> <span>=</span> <span>"ru"</span> -a -f <span>$(</span><span>echo</span> <span>"</span><span>$path</span><span>""md"</span><span>)</span> <span>]</span><span>;</span> <span>then</span>
                <span>local</span> <span>path</span><span>=</span><span>$(</span><span>echo</span> <span>"</span><span>$path</span><span>""md"</span><span>)</span>
        <span>else</span>
                <span>local</span> <span>path</span><span>=</span><span>$(</span><span>echo</span> <span>"</span><span>$path</span><span>""</span><span>$lang</span><span>"".md"</span><span>)</span>
        <span>fi</span>

        <span>if</span> ! <span>[</span> -f <span>"</span><span>$path</span><span>"</span> <span>]</span><span>;</span> <span>then</span>
                <span>local</span> <span>path</span><span>=</span>
        <span>fi</span>

        <span>eval</span> <span>$__variable</span><span>=</span><span>"'</span><span>$path</span><span>'"</span>
<span>}</span>

fmadd<span>()</span> <span>{</span>
        <span>if</span> ! <span>[</span> -f <span>"</span><span>$1</span><span>"</span> <span>]</span><span>;</span> <span>then</span>
                <span>return</span> <span>1</span>
        <span>fi</span>

        sed -i <span>'/^\+\+\+$/,/^\+\+\+$/{
</span><span>                \|'</span><span>"</span><span>$3</span><span>"</span><span>'|!{
</span><span>                        s|^\(\s*'</span><span>"</span><span>$2</span><span>"</span><span>'\s*=\s*\[..*\)\s*\]|\1, \"'</span><span>"</span><span>$3</span><span>"</span><span>'\"\]|g
</span><span>                }
</span><span>        }'</span> <span>"</span><span>$1</span><span>"</span>

        sed -i <span>'2,/^\+\+\+$/{
</span><span>                /^\s*'</span><span>"</span><span>$2</span><span>"</span><span>'\s*=/{
</span><span>                        h
</span><span>                        s|\(\s*'</span><span>"</span><span>$2</span><span>"</span><span>'\s*=\s*\[\)\s*\]|\1\"'</span><span>"</span><span>$3</span><span>"</span><span>'\"\]|g
</span><span>                }
</span><span>                /^\+\+\+$/{
</span><span>                        x
</span><span>                        /^$/{
</span><span>                                x
</span><span>                                /^\+\+\+$/i '</span><span>"</span><span>$2</span><span>"</span><span>' = \[\"'</span><span>"</span><span>$3</span><span>"</span><span>'\"\]
</span><span>                                x
</span><span>                        }
</span><span>                x
</span><span>                }
</span><span>        }'</span> <span>"</span><span>$1</span><span>"</span>
<span>}</span>

findfile filename <span>"</span><span>$1</span><span>"</span>
fmadd <span>"</span><span>$filename</span><span>"</span> <span>"</span><span>$2</span><span>"</span> <span>"</span><span>$3</span><span>"</span>
</code></pre>


<p>The <code>findfile()</code> function locates a file that resides in the <code>content</code> directory, and for a page URL like <code>/en/posts/2021/syndication/</code> can be either <code>posts/2021/syndication.en.md</code> or <code>posts/2021/syndication/index.en.md</code>. For a Russian version there would be no <code>/en</code> at the beginning of the URL, and either <code>filename.ru.md</code> or <code>filename.md</code> would work. It returns the empty path if the file can’t be located.</p>
<p>The <code>fmadd()</code> function uses some heavy <code>sed</code> magic to add the necessary URL to the front matter. The first <code>sed</code> works when there already are some values in the necessary string, and the second <code>sed</code> is for the cases when the array is initially empty, or the string isn’t there at all.</p>
<p>This approach is far from universal, of course, but it works in my setup. And, hopefully, this post will be of some use to those of you who look to solve a similar problem.</p>]]></description>
</item></channel>
</rss>