www
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYestin L. Harrison <yestin@ylh.io>2022-06-26 12:32:06 -0700
committerYestin L. Harrison <yestin@ylh.io>2022-06-26 12:41:45 -0700
commit96e5b0469d3dfa1a19ef1247f73043e9281661db (patch)
tree1e4f200ecf79c31a78a6ddbd9dfaf75481ed24b5
downloadperc-96e5b0469d3dfa1a19ef1247f73043e9281661db.tar.gz
perc-96e5b0469d3dfa1a19ef1247f73043e9281661db.tar.xz
perc-96e5b0469d3dfa1a19ef1247f73043e9281661db.zip
initial commit
-rw-r--r--.gitignore1
-rw-r--r--LICENSE13
-rw-r--r--Main.hs50
-rw-r--r--README.md23
-rw-r--r--default.nix27
-rw-r--r--perc.cabal19
-rw-r--r--perc.nix12
7 files changed, 145 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..2022b2b
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/dist-newstyle/
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..1cce46d
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,13 @@
+Copyright (c) 2022 Yestin L. Harrison <yestin@ylh.io>
+
+Permission to use, copy, modify, and distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. \ No newline at end of file
diff --git a/Main.hs b/Main.hs
new file mode 100644
index 0000000..d5a1a3d
--- /dev/null
+++ b/Main.hs
@@ -0,0 +1,50 @@
+module Main where
+
+import Control.Arrow
+import Data.ByteString (hGetContents)
+import Data.Default
+import Data.Either
+import Data.Functor
+import Data.Text.Encoding
+import Prelude hiding (hGetContents, putStr, lines, unlines, concat)
+import Data.Text (pack, concat, lines, unlines, Text)
+import Data.Text.IO (putStr)
+import System.Environment
+import System.IO (openFile, IOMode(..), stdin)
+import Text.Pandoc.Class
+import Text.Pandoc.Error
+import Text.Pandoc.Logging
+import Text.Pandoc.Options
+import Text.Pandoc.Readers.Markdown
+import Text.Pandoc.Writers.HTML
+
+mkSource name = (>>= hGetContents) >>> fmap (decodeUtf8 >>> (name,))
+
+setSource (path:_) = mkSource path (openFile path ReadMode)
+setSource _ = mkSource "<standard input>" (return stdin)
+
+shouldReport NoTranslation{} = False
+shouldReport CouldNotLoadTranslations{} = False
+shouldReport msg = messageVerbosity msg == WARNING
+
+logPretty msg =
+ case lines (showLogMessage msg) of
+ [] -> []
+ l:ls -> concat ["[", verbosity msg, "] ", l] : map ("\t"<>) ls
+ where verbosity = pack . show . messageVerbosity
+
+doPandoc :: (FilePath, Text) -> PandocPure Text
+doPandoc source =
+ readMarkdown def{readerExtensions = pandocExtensions} [source]
+ >>= writeHtml5String def{writerHTMLMathMethod = MathML}
+ >>= \t -> getLog <&> filter shouldReport <&> \case
+ [] -> t
+ ms ->
+ unlines (t:"<pre class=\"warning\">":concatMap logPretty ms)
+ <> "</pre>\n"
+
+doError = flip either id $
+ renderError >>> (:["</pre>"]) >>> ("<pre class=\"error\">":) >>> unlines
+
+main :: IO ()
+main = getArgs >>= setSource <&> (doPandoc >>> runPure >>> doError) >>= putStr
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..6e1682c
--- /dev/null
+++ b/README.md
@@ -0,0 +1,23 @@
+perc - pandoc for werc
+======================
+
+perc is a slimmed-down Pandoc-markdown-to-HTML converter suitable for use with
+werc.
+
+It accepts an optional argument to read; otherwise, it reads standard input. It
+deals strictly with (extended) markdown input and HTML output; when all
+transitive dependencies are built with `-split-sections`, `perc` is about half
+the size of the full `pandoc` executable. In addition, it runs in the
+`PandocPure` monad, ensuring no bizarre edge cases that reach out to the
+outside world.
+
+MathML for equations is turned on by default, and pure Pandoc errors/warnings
+are embedded in `<pre>` tags for ease of debugging.
+
+Licensing
+---------
+
+While the source code in this repository is permissively (ISC) licensed, keep
+in mind that builds will be statically linked against Pandoc and will therefore
+be under GPL v2 or (at your option) higher, the same as Pandoc.
+
diff --git a/default.nix b/default.nix
new file mode 100644
index 0000000..6ca3bc2
--- /dev/null
+++ b/default.nix
@@ -0,0 +1,27 @@
+# toplevel callPackage
+{ lib, haskell, haskellPackages, removeReferencesTo }: let
+ inherit (haskellPackages) callPackage pandoc-types file-embed;
+ inherit (haskell.lib.compose) overrideCabal;
+ addEmbed = { configureFlags ? [], buildDepends ? [], ... }: {
+ configureFlags = configureFlags ++ [ "-fembed_data_files" ];
+ buildDepends = buildDepends ++ [ file-embed ];
+ };
+ pandoc = overrideCabal addEmbed haskellPackages.pandoc;
+ perc' = callPackage ./perc.nix {
+ inherit pandoc;
+ };
+ addRemove =
+ { pname, buildTools ? [], postInstall ? "", postFixup ? "", ... }: {
+ buildTools = buildTools ++ [ removeReferencesTo ];
+ postInstall = ''
+ ${postInstall}
+ remove-references-to -t ${pandoc} -t ${pandoc-types} $out/bin/${pname}
+ '';
+ postFixup = ''
+ ${postFixup}
+ pushd $out
+ ls | grep -xv bin | xargs rm -rf
+ popd
+ '';
+ };
+in overrideCabal addRemove perc'
diff --git a/perc.cabal b/perc.cabal
new file mode 100644
index 0000000..18138cd
--- /dev/null
+++ b/perc.cabal
@@ -0,0 +1,19 @@
+cabal-version: 2.4
+
+name: perc
+version: 1.0
+synopsis: markdown to html for werc
+description: see README.md
+
+license: ISC
+license-file: LICENSE
+author: Yestin L. Harrison
+maintainer: yestin@ylh.io
+extra-source-files: README.md
+
+executable perc
+ hs-source-dirs: .
+ main-is: Main.hs
+ build-depends: base, pandoc, bytestring, text, data-default
+ default-language: Haskell2010
+ default-extensions: OverloadedStrings, LambdaCase, TupleSections
diff --git a/perc.nix b/perc.nix
new file mode 100644
index 0000000..91a1e33
--- /dev/null
+++ b/perc.nix
@@ -0,0 +1,12 @@
+# haskellPackages.callPackage
+{ lib, mkDerivation, base, bytestring, text, pandoc, data-default }:
+mkDerivation {
+ pname = "perc";
+ version = "1.0";
+ src = ./.;
+
+ libraryHaskellDepends = [ base bytestring text pandoc data-default ];
+ isLibrary = false;
+
+ license = lib.licenses.isc;
+}