Skip to content

Instantly share code, notes, and snippets.

@deltam
Created July 1, 2018 11:20
Show Gist options
  • Save deltam/8b4ef7e250816007993fe17d4b8c3f4d to your computer and use it in GitHub Desktop.
Save deltam/8b4ef7e250816007993fe17d4b8c3f4d to your computer and use it in GitHub Desktop.
Convert changelog memo to JSON in clojure.
(ns sandbox.changelog
(:require [clojure.string :refer [split blank? includes?]]
[clojure.java.io :refer [reader]]))
(def df (java.text.SimpleDateFormat. "yyyy-MM-dd"))
(defn header? [line]
(not (nil? (re-find #"^\d{4}-\d{2}-\d{2}" line))))
(defn parse-header [line]
(if (header? line)
(let [[date name mail] (split line #" ")]
{:date (.parse df date)
:date-txt date
:name name
:mail mail})))
(defn title? [line]
(not (nil? (re-find #"^ +\* .+: .+$" line))))
(defn parse-title [line]
(if (title? line)
(let [[_ tags title] (re-find #"^ +\* (.+): (.+)$" line)]
{:tags tags
:title title})))
(defn parse-content [lines header]
(map (fn [[[title] txt]] (merge header (parse-title title) {:memo txt}))
(partition 2 (partition-by title? lines))))
(defn parse [filename]
(let [lines (filter #(not (blank? %)) (line-seq (reader filename)))]
(mapcat (fn [[[h] cont]] (parse-content cont (parse-header h)))
(partition 2 (partition-by header? lines)))))
(defn filter-tag [parsed tag]
(filter (fn [{t :tags}] (includes? t tag)) parsed))
(defn filter-since [parsed date]
(let [d (.parse df date)]
(take-while (fn [{cd :date}] (<= (.compareTo d cd) 0))
parsed)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment