diff options
Diffstat (limited to 'tprint/src/Main.hs')
-rw-r--r-- | tprint/src/Main.hs | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/tprint/src/Main.hs b/tprint/src/Main.hs index f6008ac..aed0388 100644 --- a/tprint/src/Main.hs +++ b/tprint/src/Main.hs | |||
@@ -140,25 +140,9 @@ tprint TPrint{ operation = DraftCreate{..}, ..} Client{..} out = withPrintout in | |||
140 | tprint TPrint{ operation = DraftReplace{..}, ..} Client{..} _ = withPrintout input $ \p -> do | 140 | tprint TPrint{ operation = DraftReplace{..}, ..} Client{..} _ = withPrintout input $ \p -> do |
141 | unless dryRun $ draftReplace draftId draftTitle p | 141 | unless dryRun $ draftReplace draftId draftTitle p |
142 | 142 | ||
143 | tprint TPrint{ operation = DraftEdit{..}, ..} Client{..} _ = do | 143 | tprint TPrint{ operation = DraftEdit{..}, ..} Client{..} out = do |
144 | d@(fromMaybe T.empty -> dTitle, dContent) <- draft draftId | 144 | (dTitle, dContent) <- editDraft (fst output) =<< maybe (return mempty) draft mDraftId |
145 | let | 145 | unless dryRun $ maybe (\t c -> hPutStrLn out . show =<< draftCreate t c) draftReplace mDraftId dTitle dContent |
146 | content | ||
147 | | (JSON, _) <- output = return . LBS.toStrict $ encodePretty d | ||
148 | | otherwise = do | ||
149 | c <- either throwM return (cobbcode dContent) | ||
150 | return . T.encodeUtf8 $ dTitle <> T.pack "\n" <> c | ||
151 | tmpl | ||
152 | | (JSON, _) <- output = jsonTemplate | ||
153 | | otherwise = plainTemplate | ||
154 | parse | ||
155 | | (JSON, _) <- output = either (throwM . userError) return . eitherDecodeStrict' | ||
156 | | otherwise = (\(t:(T.unlines -> c)) -> (massage t, ) <$> either throwM return (bbcode c)) . T.lines . T.decodeUtf8 | ||
157 | massage (T.strip -> t) | ||
158 | | T.null t = Nothing | ||
159 | | otherwise = Just t | ||
160 | (dTitle', dContent') <- parse =<< runUserEditorDWIM tmpl =<< content | ||
161 | unless dryRun $ draftReplace draftId dTitle' dContent' | ||
162 | 146 | ||
163 | tprint TPrint{ operation = DraftDelete{..}, ..} Client{..} _ = unless dryRun $ draftDelete draftId | 147 | tprint TPrint{ operation = DraftDelete{..}, ..} Client{..} _ = unless dryRun $ draftDelete draftId |
164 | 148 | ||
@@ -170,3 +154,23 @@ tprint TPrint{ operation = DraftPrint{..}, ..} client@Client{..} out = do | |||
170 | 154 | ||
171 | 155 | ||
172 | tprint _ _ _ = undefined | 156 | tprint _ _ _ = undefined |
157 | |||
158 | |||
159 | editDraft :: Format -> (Maybe DraftTitle, Printout) -> IO (Maybe DraftTitle, Printout) | ||
160 | editDraft fmt d@(fromMaybe T.empty -> dTitle, dContent) = do | ||
161 | let | ||
162 | content | ||
163 | | JSON <- fmt = return . LBS.toStrict $ encodePretty d | ||
164 | | otherwise = do | ||
165 | c <- either throwM return (cobbcode dContent) | ||
166 | return . T.encodeUtf8 $ dTitle <> T.pack "\n--- ^ Title ---\n" <> c | ||
167 | tmpl | ||
168 | | JSON <- fmt = jsonTemplate | ||
169 | | otherwise = plainTemplate | ||
170 | parse | ||
171 | | JSON <- fmt = either (throwM . userError) return . eitherDecodeStrict' | ||
172 | | otherwise = (\(t:_:(T.unlines -> c)) -> (massage t, ) <$> either throwM return (bbcode c)) . T.lines . T.decodeUtf8 | ||
173 | massage (T.strip -> t) | ||
174 | | T.null t = Nothing | ||
175 | | otherwise = Just t | ||
176 | parse =<< runUserEditorDWIM tmpl =<< content | ||