Mastodon Archiv

Nachdem ich letztents ein Archiv meiner Mastodon Posts erstellt habe…

…sind diese nun auch Teil dieser Website. Das Archiv ist bisher noch nicht im Menü verkinkt, es ist unter /mastodon erreichbar.

Umsetzung

Die Umsetzung ist als Hugo Content Adapter recht einfach realisiert. Dieser liest das im letzten Vorgänger-Post erstellte Archiv (als JSON) ein und erstellt daraus Repräsentationen im Hugo-internen Seitenformat. Dazu muss im Hugo content Verzeichnis ein leeres Unterverzeichnis mit einer Datei namens _content.gotmpl angelegt werden. Das Beispiel nimmt dafür den Namen mastodon/_content.gotmpl an.

Am Anfang der Datei müssen lediglich die Quellldatei ($mastodonFile) und der Ordner, in dem die Medien abgelegt sind ($mastodonMediaPath), angegeben werden.

{{- $mastodonFile := "mastodon/mastodon-archive.json" -}}
{{- $mastodonMediaPath := "mastodon/media/" -}}
{{- $adapter := . -}}

{{- $data := dict -}}
{{- warnf "[mastodon/_content.gotmpl] Generating content from %s" $mastodonFile -}}
{{- with resources.Get $mastodonFile -}}
  {{ $data = . | transform.Unmarshal }}
{{- else -}}
  {{- errorf "mastodon/_content.gotmpl] Failed to load %s" $mastodonFile -}}
{{- end -}}
{{- .EnableAllLanguages -}}

{{- $structures := slice "statuses" "favourites" "mentions" -}}
{{- range $structure := $structures -}}

  {{- range (index $data $structure) -}}

    {{- $account := .account.username -}}
    {{- $content := .content -}}
    {{- $createdAt := .created_at -}}
    {{- $id := .id -}}
    {{- $media_attachments := slice -}}
    {{- with .media_attachments -}}
      {{- range . -}}
        {{- $mediaType := .type -}}
        {{- $url := .url -}}
        {{- $localMedia := path.Join $mastodonMediaPath (urls.Parse .url).Path -}}
        {{- if os.FileExists (path.Join "assets" $localMedia) -}}
          {{- $url := $localMedia -}}
        {{- else -}}
          {{- warnf "[mastodon/_content.gotmpl] Local media file not found: %s" $localMedia -}}
        {{- end -}}
        {{- $previewUrl := .preview_url -}}
        {{- $localPreview := path.Join $mastodonMediaPath (urls.Parse .preview_url).Path -}}
        {{- if os.FileExists (path.Join "assets" $localPreview) -}}
          {{- $previewUrl := $localPreview -}}
        {{- else -}}
          {{- warnf "[mastodon/_content.gotmpl] Local preview file not found: %s" $localPreview -}}
        {{- end -}}
        {{- $description := .description -}}
        {{- $media_attachments = $media_attachments | append (dict "type" $mediaType "url" $url "preview_url" $previewUrl "description" $description) -}}
      {{- end -}}
    {{- end -}}
    {{- $reblog := slice -}}
    {{- with .reblog -}}
      {{- range . -}}
        {{- $reblog = $reblog | append . -}}
      {{- end -}}
    {{- end -}}
    {{- $language := .language -}}
    {{- $reblogsCount := .reblogs_count -}}
    {{- $favouritesCount := .favourites_count -}}
    {{- $card := dict -}}
    {{- with .card -}}
      {{- $url := .url -}}
      {{- $title := .title -}}
      {{- $description := .description -}}
      {{- $card = dict "url" $url "title" $title "description" $description -}}
    {{- end -}}
    {{- $url := .url -}}
    {{- $repliesCount := .replies_count -}}
    {{- $tags := slice -}}
    {{- with .tags -}}
      {{- range . -}}
        {{- $name := .name -}}
        {{- $url := .url -}}
        {{- $tags = $tags | append (dict "name" $name "url" $url) -}}
      {{- end -}}
    {{- end -}}
    {{- $mentions := .mentions -}}
    {{- $reply := dict -}}
    {{- if and (ne .in_reply_to_id nil) -}}
      {{- $reply = dict "in_reply_to_id" .in_reply_to_id "in_reply_to_account_id" .in_reply_to_account_id -}}
    {{- end -}}
    {{- $poll := .poll -}}
    {{- $pinned := .pinned -}}
    {{- $favourited := .favourited -}}
    {{- $sensitive := .sensitive -}}
    {{- $spoilerText := .spoiler_text -}}

    {{ $content := dict
      "mediaType" "text/html"
      "value" $content
    }}
    {{ $page := dict
      "content" $content
      "kind" "page"
      "date" (time.AsTime $createdAt)
      "params" (dict
        "account" $account
        "created_at" $createdAt
        "id" $id
        "media_attachments" $media_attachments
        "language" $language
        "reblogs_count" $reblogsCount
        "favourites_count" $favouritesCount
        "card" $card
        "url" $url
        "replies_count" $repliesCount
        "_tags" $tags
        "reblog" $reblog
        "mentions" $mentions
        "reply" $reply
        "poll" $poll
        "pinned" $pinned
        "favourited" $favourited
        "sensitive" $sensitive
        "type" $structure
        "spoiler_text" $spoilerText
      )
      "path" $id
    }}
    {{ $adapter.AddPage $page }}
  {{- end -}}
{{- end -}}