|
| 1 | +#!/usr/bin/env stack |
| 2 | +-- stack --install-ghc runghc --package turtle --package projectroot --package text --package wreq |
| 3 | +{-# LANGUAGE OverloadedStrings #-} |
| 4 | +{-# LANGUAGE LambdaCase #-} |
| 5 | + |
| 6 | +import qualified Data.Text as Text |
| 7 | +import qualified Data.Text.IO as Text |
| 8 | +import Prelude hiding (FilePath) |
| 9 | +import System.Environment (getArgs) |
| 10 | +import Turtle |
| 11 | + |
| 12 | +main :: IO () |
| 13 | +main = getArgs >>= \case |
| 14 | + "convert":inp:out:_ -> do |
| 15 | + ec <- shell ("cat " <> Text.pack inp <> " |" |
| 16 | + <> "sed 's/{{< \\/highlight >}}/{% endhighlight %}/' |" |
| 17 | + <> "sed 's/{{< highlight /{% highlight /' |" |
| 18 | + <> "sed 's/>}}/%}/'" |
| 19 | + <> " > " <> Text.pack out |
| 20 | + ) empty |
| 21 | + case ec of |
| 22 | + ExitSuccess -> |
| 23 | + echo "Converted." |
| 24 | + failure -> exit failure |
| 25 | + "new":_ -> do |
| 26 | + putStr "Paste the URL for the post you want to translate: " |
| 27 | + url <- Text.getLine |
| 28 | + echo ("Fetching " <> Text.take 80 url <> "...") |
| 29 | + ec <- shell ("curl -L -s " <> url <> " > newpost.md") empty |
| 30 | + case ec of |
| 31 | + ExitSuccess -> do |
| 32 | + echo "Fetched." |
| 33 | + echo "Converting to jekyll syntax..." |
| 34 | + ec' <- shell ("cat newpost.md |" |
| 35 | + <> "sed 's/{{< \\/highlight >}}/{% endhighlight %}/' |" |
| 36 | + <> "sed 's/{{< highlight /{% highlight /' |" |
| 37 | + <> "sed 's/>}}/%}/'" |
| 38 | + <> " > newpost2.md" |
| 39 | + ) empty |
| 40 | + case ec' of |
| 41 | + ExitSuccess -> do |
| 42 | + mv "newpost2.md" "newpost.md" |
| 43 | + echo "Converted newpost.md." |
| 44 | + echo "Edit newpost.md." |
| 45 | + failure -> exit failure |
| 46 | + failure -> exit failure |
| 47 | + _ -> error "No parse of args" |
0 commit comments