]> gitweb.factorcode.org Git - factor.git/commitdiff
tldr: adding a lightweight tool to render tldr.sh pages.
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 18 Mar 2021 16:52:57 +0000 (09:52 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 18 Mar 2021 16:52:57 +0000 (09:52 -0700)
extra/tldr/authors.txt [new file with mode: 0644]
extra/tldr/summary.txt [new file with mode: 0644]
extra/tldr/tags.txt [new file with mode: 0644]
extra/tldr/tldr.factor [new file with mode: 0644]

diff --git a/extra/tldr/authors.txt b/extra/tldr/authors.txt
new file mode 100644 (file)
index 0000000..e091bb8
--- /dev/null
@@ -0,0 +1 @@
+John Benediktsson
diff --git a/extra/tldr/summary.txt b/extra/tldr/summary.txt
new file mode 100644 (file)
index 0000000..8fa4740
--- /dev/null
@@ -0,0 +1 @@
+tldr pages (simplified and community-driven man pages)
diff --git a/extra/tldr/tags.txt b/extra/tldr/tags.txt
new file mode 100644 (file)
index 0000000..ebb74b4
--- /dev/null
@@ -0,0 +1 @@
+not loaded
diff --git a/extra/tldr/tldr.factor b/extra/tldr/tldr.factor
new file mode 100644 (file)
index 0000000..0aa9d89
--- /dev/null
@@ -0,0 +1,71 @@
+! Copyright (C) 2021 John Benediktsson
+! See http://factorcode.org/license.txt for BSD license
+
+USING: assocs combinators command-line http.client io
+io.directories io.encodings.utf8 io.files io.files.temp
+io.launcher io.pathnames json.reader kernel namespaces sequences
+splitting system urls wrap.strings ;
+
+IN: tldr
+
+SYMBOL: tldr-language
+tldr-language [ "en" ] initialize
+
+SYMBOL: tldr-platform
+tldr-platform [
+    os {
+        { macosx [ "osx" ] }
+        { linux [ "linux" ] }
+        { windows [ "windows" ] }
+    } case
+] initialize
+
+<PRIVATE
+
+CONSTANT: tldr-zip URL" https://tldr-pages.github.io/assets/tldr.zip"
+
+: download-tldr ( -- )
+    "tldr" cache-file dup make-directory [
+        tldr-zip "tldr.zip" download-to
+        { "unzip" "tldr.zip" } try-process
+    ] with-directory ;
+
+: ?download-tldr ( -- )
+    "tldr/tldr.zip" cache-file exists? [ download-tldr ] unless ;
+
+MEMO: tldr-index ( -- index )
+    "tldr/index.json" cache-file path>json ;
+
+: find-command ( name -- command )
+    tldr-index "commands" of [ "name" of = ] with find nip ;
+
+: platform ( command -- platform )
+    "platform" of tldr-platform get '[ _ = ] find nip "common" or ;
+
+: language ( command -- language )
+    "language" of tldr-language get '[ _ = ] find nip "en" or ;
+
+: tldr-path ( name platform language -- path )
+    "pages" over "en" = [ nip ] [ "." glue ] if prepend-path
+    swap ".md" append append-path "tldr" cache-file prepend-path ;
+
+PRIVATE>
+
+: tldr ( name -- lines )
+    dup find-command [ platform ] [ language ] bi
+    tldr-path utf8 file-lines ;
+
+: tldr. ( name -- )
+    tldr [
+        { "`" "    " } [ ?head ] any? [
+            "`" ?tail drop 76 "  " wrap-indented-string
+        ] [
+            { "# " "= " "> " "- " } [ ?head ] any? drop
+            76 wrap-string
+        ] if print
+    ] each ;
+
+: tldr-main ( -- )
+    command-line get [ tldr. nl ] each ;
+
+MAIN: tldr-main