Skip to content

Commit 6d4f158

Browse files
committed
Document formatter
1 parent e8536ae commit 6d4f158

6 files changed

Lines changed: 48 additions & 59 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
## Unreleased
44

5-
- Revert swallowing cljs reference errors. (See [official forums](https://discourse.mozilla.org/t/which-tools-are-available-for-finding-errors-in-ftl-files/70313/2) for reasoning.)
5+
- Add an opinionated, no options formatter.
6+
- Clean up build.clj.
7+
- Bump net.xyzsd.fluent/fluent-base to 0.72.
68
- Bump @fluent/bundle to 0.19.
7-
- Add Clojure and Java formatters. Clojure is more idiomatic, Java is twice as fast. 500us vs 250us for a 2k file.
8-
- Clean up build.clj, add prep task.
9+
- Revert swallowing cljs reference errors. (See [official forums](https://discourse.mozilla.org/t/which-tools-are-available-for-finding-errors-in-ftl-files/70313/2) for reasoning.)
910

1011
## 0.0.2
1112

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ This library aims to smooth over those differences, making it easy to build your
1010
> [!NOTE]
1111
> Requires Clojure 1.12 because new interop syntax is really nice. I'm not looking to support earlier Clojures at this time.
1212
13+
## Table of Contents
14+
15+
<!-- toc -->
16+
17+
- [Getting Started](#getting-started)
18+
- [Usage](#usage)
19+
- [Formatter](#formatter)
20+
- [Extended example](#extended-example)
21+
22+
<!-- tocstop -->
23+
1324
## Getting Started
1425

1526
Add it to your deps.edn or project.clj:
@@ -18,7 +29,7 @@ Add it to your deps.edn or project.clj:
1829
{:deps {io.github.noahtheduke/fluent-clj {:mvn/version "0.0.2"}}}
1930
```
2031

21-
## Example
32+
## Usage
2233

2334
```clojure
2435
(require '[noahtheduke.fluent :as i18n])
@@ -54,7 +65,11 @@ email-cnt = {$cnt ->
5465
=> "2 emails"
5566
```
5667

57-
## Usage in an actual application
68+
## Formatter
69+
70+
A no-options formatter for Fluent Files is included, available as a `-X` function. To use it, call `clojure -X noahtheduke.fluent.utils/fmt :file "corpus/example.ftl"` to format a single file or `clojure -X noahtheduke.fluent.utils/fmt :dir "corpus"` to format all `.ftl` files in the given directory.
71+
72+
## Extended example
5873

5974
I built this library for a website that uses [Reagent](https://reagent-project.github.io), so I'll share how we do it there.
6075

build.clj

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
[clojure.java.io :as io]
88
[clojure.string :as str]
99
[clojure.tools.build.api :as b]
10-
[deps-deploy.deps-deploy :as dd])
11-
(:import
12-
[java.io File]))
10+
[deps-deploy.deps-deploy :as dd]))
1311

1412
(defn make-version []
1513
(str/trim (slurp "./resources/FLUENTCLJ_VERSION")))
@@ -29,7 +27,6 @@
2927
:src-dirs ["src"]
3028
:java-src-dirs ["src"]
3129
:resource-dirs ["resources"]
32-
:javac-opts ["--release" "17"]
3330
:pom-data [[:licenses
3431
[:license [:name "MPL-2.0"] [:url "https://mozilla.org/MPL/2.0"]]]]}
3532
opts)))
@@ -43,23 +40,9 @@
4340
(b/copy-dir {:src-dirs (concat (:src-dirs opts) (:resource-dirs opts))
4441
:target-dir (:class-dir opts)})))
4542

46-
(defn javac [opts]
47-
(let [opts (make-opts opts)]
48-
(when (->> (map io/file (:java-src-dirs opts))
49-
(mapcat file-seq)
50-
(filter #(and (.isFile ^File %)
51-
(str/ends-with? (str %) ".java")))
52-
(seq))
53-
(println "Compiling" (str/join ", " (:java-src-dirs opts)))
54-
(b/javac {:src-dirs (:java-src-dirs opts)
55-
:class-dir (:class-dir opts)
56-
:basis (:basis opts)
57-
:javac-opts (:javac-opts opts)}))))
58-
5943
(defn jar [opts]
6044
(let [opts (make-opts opts)]
6145
(copy-src opts)
62-
(javac opts)
6346
(b/jar opts)
6447
(println "Created" (str (b/resolve-path (:jar-file opts))))))
6548

@@ -71,7 +54,6 @@
7154
(defn uberjar [opts]
7255
(let [opts (make-opts opts)]
7356
(copy-src opts)
74-
(javac opts)
7557
(b/write-pom opts)
7658
(b/compile-clj opts)
7759
(b/uber opts)
@@ -81,7 +63,6 @@
8163
(let [opts (make-opts opts)]
8264
(clean opts)
8365
(copy-src opts)
84-
(javac opts)
8566
(b/write-pom opts)
8667
(b/jar opts)
8768
(dd/deploy {:installer :remote

deps.edn

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
{:paths ["src" "resources" "target/classes"]
22
:deps {org.clojure/clojure {:mvn/version "1.12.0"}
3-
net.xyzsd.fluent/fluent-base {:mvn/version "0.70"}
4-
net.xyzsd.fluent/fluent-functions-cldr {:mvn/version "0.70"}}
5-
:deps/prep-lib {:ensure "target/classes"
6-
:alias :build
7-
:fn javac}
3+
net.xyzsd.fluent/fluent-base {:mvn/version "0.72"}
4+
net.xyzsd.fluent/fluent-functions-cldr {:mvn/version "0.72"}}
85
:aliases
96
{:dev {:extra-paths ["dev"]
107
:extra-deps {criterium/criterium {:mvn/version "0.4.6"}

justfile

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
default:
22
@just --list
33

4-
@compile:
5-
clojure -T:build javac
4+
@gen-docs:
5+
markdown-toc -i --maxdepth 2 README.md
66

77
@repl arg="":
8-
just compile
98
clojure -M{{arg}}:dev:test:repl
109

1110
@run *args:
12-
just compile
1311
clojure -M:run {{args}}
1412

1513
splint:
@@ -23,7 +21,6 @@ lint:
2321
just lsp
2422

2523
test-clj:
26-
just compile
2724
clojure -M:dev:test:runner
2825

2926
test-cljs:
@@ -53,7 +50,7 @@ current_version := `cat resources/FLUENTCLJ_VERSION | xargs`
5350
# Builds the uberjar, builds the jar, sends the jar to clojars
5451
@release version:
5552
echo 'Running tests'
56-
just test-all
53+
just test
5754
echo 'Setting new version {{version}}'
5855
just set-version {{version}}
5956
echo 'Commit and tag'

src/noahtheduke/fluent/utils.clj

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,27 @@
1313
[java.io File]))
1414

1515
(defn safe-read-and-fmt
16-
^String [s]
17-
(let [contents (-> s
18-
(str/replace #"\\u([0-9A-F]{4})" "__FLUENT_CLJ__u$1")
19-
(str/replace #"\\U([0-9A-F]{4})" "__FLUENT_CLJ__U$1"))
20-
ast (parse contents)
21-
_ (when-let [errors (seq (FluentResource/.errors ast))]
22-
(throw (ex-info (str "found errors: " (ex-message (last errors))) {:errors errors})))
23-
formatted-contents (-> (pprint-str ast)
24-
(str/replace #"__FLUENT_CLJ__u([0-9A-F]{4})" "\\\\u$1")
25-
(str/replace #"__FLUENT_CLJ__U([0-9A-F]{4})" "\\\\U$1")
26-
(str/trim)
27-
(str "\n"))]
28-
formatted-contents))
16+
^String [contents]
17+
(let [ast (parse contents)]
18+
(when-let [errors (seq (FluentResource/.errors ast))]
19+
(throw (ex-info (str "found errors: " (ex-message (last errors))) {:errors errors})))
20+
(-> (pprint-str ast)
21+
(str/trim)
22+
(str "\n"))))
2923

30-
(defn fmt-dir
31-
"Format all files in a directory"
32-
[{:keys [dir]}]
33-
(doseq [f (->> (io/file (name dir))
34-
(file-seq)
35-
(filter #(File/.isFile %))
36-
(filter #(str/ends-with? (str %) ".ftl")))
37-
:let [formatted-file (safe-read-and-fmt (slurp f))]]
38-
(spit f formatted-file)))
24+
(defn fmt
25+
"Format files in :dir or single file at :file. No-op if given neither."
26+
[{:keys [dir file]}]
27+
(cond
28+
dir (doseq [f (->> (io/file (name dir))
29+
(file-seq)
30+
(filter #(File/.isFile %))
31+
(filter #(str/ends-with? (str %) ".ftl")))
32+
:let [formatted-file (safe-read-and-fmt (slurp f))]]
33+
(spit f formatted-file))
34+
file (as-> (slurp file) $
35+
(safe-read-and-fmt $)
36+
(spit file $))))
3937

4038
(comment
41-
(fmt-dir {:dir "corpus"}))
39+
(fmt {:dir "corpus"}))

0 commit comments

Comments
 (0)