-
-
Notifications
You must be signed in to change notification settings - Fork 323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor embedded files #2350
Open
thomie
wants to merge
5
commits into
simonmichael:master
Choose a base branch
from
thomie:refactor-embedded-files
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Refactor embedded files #2350
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6c82b12
;dev: do not embed hledger-ui/web manuals in hledger
thomie 8137af8
;ref: delete unused function
thomie 038e438
;ref: move DocFiles helpers to hledger-lib
thomie b8be07e
;dev: add Hledger.UI.DocFiles
thomie f39e4c9
;dev: add Hledger.Web.DocFiles
thomie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-| | ||
|
||
Helpers for viewing documentation files in various formats. | ||
|
||
|-} | ||
|
||
module Hledger.Utils.DocFiles ( | ||
Topic | ||
,TldrPage | ||
|
||
,printHelpForTopic' | ||
,runManForTopic' | ||
,runInfoForTopic' | ||
,runPagerForTopic' | ||
,runTldrForPage' | ||
|
||
) where | ||
|
||
import Control.Exception | ||
import Data.ByteString (ByteString) | ||
import qualified Data.ByteString.Char8 as BC | ||
import Data.Maybe (fromMaybe) | ||
import System.Environment (setEnv) | ||
import System.IO | ||
import System.IO.Temp | ||
import System.Process | ||
|
||
import Hledger.Utils.IO (error') | ||
import Text.Printf (printf) | ||
import System.Environment (lookupEnv) | ||
import Hledger.Utils.Debug | ||
|
||
-- The name of any hledger executable. | ||
type Tool = String | ||
|
||
-- Any heading in the hledger user manual (and perhaps later the hledger-ui/hledger-web manuals). | ||
type Topic = String | ||
|
||
-- Any name of a hledger tldr page (hledger, hledger-ui, hledger-print etc.) | ||
type TldrPage = String | ||
|
||
-- | Print plain text help for this tool. | ||
-- Takes an optional topic argument for convenience but it is currently ignored. | ||
printHelpForTopic' :: ByteString -> Maybe Topic -> IO () | ||
printHelpForTopic' b _mtopic = BC.putStr b | ||
|
||
-- | Display an info manual for this topic, opened at the given topic if provided, | ||
-- using the "info" executable in $PATH. | ||
-- Topic can be an exact heading or a heading prefix; info will favour an exact match. | ||
runInfoForTopic' :: Tool -> ByteString -> Maybe Topic -> IO () | ||
runInfoForTopic' tool b mtopic = | ||
withSystemTempFile ("hledger-"++tool++".info") $ \f h -> do | ||
BC.hPutStrLn h b | ||
hClose h | ||
callCommand $ dbg1 "info command" $ | ||
"info -f " ++ f ++ maybe "" (printf " -n '%s'") mtopic | ||
|
||
-- less with any vertical whitespace squashed, case-insensitive searching, the $ regex metacharacter accessible as \$. | ||
less = "less -s -i --use-backslash" | ||
|
||
-- | Display plain text help for this tool, scrolled to the given topic if any, using the users $PAGER or "less". | ||
-- When a topic is provided we always use less, ignoring $PAGER. | ||
-- | ||
-- This is less robust than the newer Hledger.Utils.IO.runPager, | ||
-- but that one doesn't yet support scrolling to a topic. | ||
runPagerForTopic' :: Tool -> ByteString -> Maybe Topic -> IO () | ||
runPagerForTopic' tool b mtopic = do | ||
withSystemTempFile ("hledger-"++tool++".txt") $ \f h -> do | ||
BC.hPutStrLn h b | ||
hClose h | ||
envpager <- fromMaybe less <$> lookupEnv "PAGER" | ||
let | ||
exactmatch = True | ||
(pager, searcharg) = | ||
case mtopic of | ||
Nothing -> (envpager, "") | ||
Just t -> (less, "-p'^( )?" ++ t ++ if exactmatch then "\\$'" else "") | ||
callCommand $ dbg1 "pager command" $ unwords [pager, searcharg, f] | ||
|
||
-- | Display a man page for this tool, scrolled to the given topic if provided, using "man". | ||
-- When a topic is provided we force man to use "less", ignoring $MANPAGER and $PAGER. | ||
runManForTopic' :: Tool -> ByteString -> Maybe Topic -> IO () | ||
runManForTopic' tool b mtopic = | ||
-- This temp file path should have a slash in it, man requires at least one. | ||
withSystemTempFile ("hledger-"++tool++".1") $ \f h -> do | ||
BC.hPutStrLn h b | ||
hClose h | ||
let | ||
exactmatch = True | ||
pagerarg = | ||
case mtopic of | ||
Nothing -> "" | ||
Just t -> "-P \"" ++ less ++ " -p'^( )?" ++ t ++ (if exactmatch then "\\\\$" else "") ++ "'\"" | ||
callCommand $ dbg1 "man command" $ unwords ["man", pagerarg, f] | ||
|
||
-- | Display one of the tldr pages, using "tldr". | ||
runTldrForPage' :: [(TldrPage, ByteString)] -> TldrPage -> IO () | ||
runTldrForPage' tldrs name = | ||
case lookup name tldrs of | ||
Nothing -> error' $ "sorry, there's no " <> name <> " tldr page yet" | ||
Just b -> (do | ||
withSystemTempFile (name++".md") $ \f h -> do | ||
BC.hPutStrLn h b | ||
hClose h | ||
-- tldr clients tend to auto-update their data, try to discourage that here | ||
-- tealdeer - doesn't auto-update by default | ||
-- tlrc - ? | ||
-- tldr-node-client - undocumented env var suggested in output | ||
setEnv "TLDR_AUTO_UPDATE_DISABLED" "1" | ||
callCommand $ dbg1 "tldr command" $ "tldr --render " <> f | ||
) `catch` (\(_e::IOException) -> do | ||
hPutStrLn stderr $ "Warning: could not run tldr --render, using fallback viewer instead.\n" | ||
BC.putStrLn b | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{-# LANGUAGE TemplateHaskell #-} | ||
{-| | ||
|
||
Embedded documentation files in various formats, and helpers for viewing them. | ||
|
||
|-} | ||
|
||
module Hledger.UI.DocFiles ( | ||
printHelpForTopic | ||
,runManForTopic | ||
,runInfoForTopic | ||
,runPagerForTopic | ||
,runTldrForPage | ||
) where | ||
|
||
import Data.ByteString (ByteString) | ||
|
||
import Hledger.Utils (embedFileRelative) | ||
import Hledger.Utils.DocFiles | ||
|
||
-- | All hledger-ui pages from the tldr-pages project. | ||
tldrs :: [(TldrPage, ByteString)] | ||
tldrs = [ | ||
("hledger-ui", $(embedFileRelative "embeddedfiles/hledger-ui.md")) | ||
] | ||
|
||
-- | The main hledger-ui manuals as source for man, info and as plain text. | ||
man :: ByteString | ||
man = $(embedFileRelative "embeddedfiles/hledger-ui.1") | ||
txt :: ByteString | ||
txt = $(embedFileRelative "embeddedfiles/hledger-ui.txt") | ||
info :: ByteString | ||
info = $(embedFileRelative "embeddedfiles/hledger-ui.info") | ||
|
||
printHelpForTopic :: Maybe Topic -> IO () | ||
printHelpForTopic = printHelpForTopic' txt | ||
|
||
runManForTopic :: Maybe Topic -> IO () | ||
runManForTopic = runManForTopic' "hledger-ui" man | ||
|
||
runInfoForTopic :: Maybe Topic -> IO () | ||
runInfoForTopic = runInfoForTopic' "hledger-ui" info | ||
|
||
runPagerForTopic :: Maybe Topic -> IO () | ||
runPagerForTopic = runPagerForTopic' "hledger-ui" txt | ||
|
||
runTldrForPage :: TldrPage -> IO () | ||
runTldrForPage = runTldrForPage' tldrs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../hledger-ui.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../hledger-ui.info |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../hledger-ui.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{-# LANGUAGE TemplateHaskell #-} | ||
{-| | ||
|
||
Embedded documentation files in various formats, and helpers for viewing them. | ||
|
||
|-} | ||
|
||
module Hledger.Web.DocFiles ( | ||
printHelpForTopic | ||
,runManForTopic | ||
,runInfoForTopic | ||
,runPagerForTopic | ||
,runTldrForPage | ||
) where | ||
|
||
import Data.ByteString (ByteString) | ||
|
||
import Hledger.Utils (embedFileRelative) | ||
import Hledger.Utils.DocFiles | ||
|
||
-- | All hledger-web pages from the tldr-pages project. | ||
tldrs :: [(TldrPage, ByteString)] | ||
tldrs = [ | ||
("hledger-web", $(embedFileRelative "embeddedfiles/hledger-web.md")) | ||
] | ||
|
||
-- | The main hledger-web manuals as source for man, info and as plain text. | ||
man :: ByteString | ||
man = $(embedFileRelative "embeddedfiles/hledger-web.1") | ||
txt :: ByteString | ||
txt = $(embedFileRelative "embeddedfiles/hledger-web.txt") | ||
info :: ByteString | ||
info = $(embedFileRelative "embeddedfiles/hledger-web.info") | ||
|
||
printHelpForTopic :: Maybe Topic -> IO () | ||
printHelpForTopic = printHelpForTopic' txt | ||
|
||
runManForTopic :: Maybe Topic -> IO () | ||
runManForTopic = runManForTopic' "hledger-web" man | ||
|
||
runInfoForTopic :: Maybe Topic -> IO () | ||
runInfoForTopic = runInfoForTopic' "hledger-web" info | ||
|
||
runPagerForTopic :: Maybe Topic -> IO () | ||
runPagerForTopic = runPagerForTopic' "hledger-web" txt | ||
|
||
runTldrForPage :: TldrPage -> IO () | ||
runTldrForPage = runTldrForPage' tldrs |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, changing the location of the man pages (from
./hledger-ui.1
to./embeddedfiles/hledger-ui.1
) will probably cause busy work for package maintainers.Perhaps I should just leave the files where they were.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW I suspect the majority of packagers get man pages from a github or hackage tarball.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or at least, I'm not too worried about this. Will look.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, so this changes the hackage tarball I believe.
Compare
hledger: https://hackage.haskell.org/package/hledger-1.42/src/
hledger-ui: https://hackage.haskell.org/package/hledger-ui-1.42/src/
The man pages in the hledger tarball are inside the subdirectory embeddedfiles, and so package maintainers have to find them there, while the man pages in hledger-ui are currently in the toplevel directory.
We should probably not change this setup without a good reason.