docbook2txt: rename `xmldoc2txt` to `docbook2txt`

This commit is contained in:
Oystein Kristoffer Tveit 2022-11-29 23:16:52 +01:00
parent 33fcdf7ab9
commit ea4e35b82a
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146
5 changed files with 31 additions and 22 deletions

View File

@ -25,9 +25,9 @@
type = "app"; type = "app";
program = "${self.packages.${system}.nix2json}/bin/nix2json"; program = "${self.packages.${system}.nix2json}/bin/nix2json";
}; };
xmldoc2txt = { docbook2txt = {
type = "app"; type = "app";
program = "${self.packages.${system}.xmldoc2txt}/bin/xmldoc2txt"; program = "${self.packages.${system}.docbook2txt}/bin/docbook2txt";
}; };
}; };
@ -40,14 +40,14 @@
home-manager-search = home-manager-search =
pkgs.callPackage ./searchers/home-manager-search.nix { pkgs.callPackage ./searchers/home-manager-search.nix {
inherit home-manager; inherit home-manager;
inherit (self.packages.${system}) json2nix xmldoc2txt; inherit (self.packages.${system}) json2nix docbook2txt;
defaultManualPath = defaultManualPath =
let pkg = self.packages.${system}.home-manager-json; let pkg = self.packages.${system}.home-manager-json;
in "${pkg}/share/doc/home-manager/options.json"; in "${pkg}/share/doc/home-manager/options.json";
}; };
nix-option-search = pkgs.callPackage ./searchers/nix-option-search.nix { nix-option-search = pkgs.callPackage ./searchers/nix-option-search.nix {
inherit nixpkgs; inherit nixpkgs;
inherit (self.packages.${system}) json2nix xmldoc2txt; inherit (self.packages.${system}) json2nix docbook2txt;
defaultManualPath = defaultManualPath =
let pkg = self.packages.${system}.nix-options-json; let pkg = self.packages.${system}.nix-options-json;
in "${pkg}/share/doc/nixos/options.json"; in "${pkg}/share/doc/nixos/options.json";
@ -67,8 +67,8 @@
# Internal Tools # Internal Tools
json2nix = json2nix =
pkgs.callPackage ./internals/json2nix { compiler = "ghc924"; }; pkgs.callPackage ./internals/json2nix { compiler = "ghc924"; };
xmldoc2txt = docbook2txt =
pkgs.callPackage ./internals/xmldoc2txt { compiler = "ghc924"; }; pkgs.callPackage ./internals/docbook2txt { compiler = "ghc924"; };
}; };
overlays.default = _: prev: prev // self.packages.${system}; overlays.default = _: prev: prev // self.packages.${system};

View File

@ -1,4 +1,4 @@
{ pkgs, compiler ? "ghc924", ... }: { pkgs, compiler ? "ghc924", ... }:
pkgs.writers.writeHaskellBin "xmldoc2txt" { pkgs.writers.writeHaskellBin "docbook2txt" {
libraries = with pkgs.haskellPackages; [ tagsoup ansi-terminal split text ]; libraries = with pkgs.haskellPackages; [ tagsoup ansi-terminal split text ];
} (builtins.readFile ./xmldoc2txt.hs) } (builtins.readFile ./docbook2txt.hs)

View File

@ -1,3 +1,14 @@
-- This is a program that converts docbook xml to optionally ANSI colored
-- raw text.
--
-- See https://tdg.docbook.org/ for more information about the docbook format.
--
-- This conversion could also be achieved by using pandoc.
-- However because of the custom color formatting, we would probably
-- end up having to write custom conversion logic for every tag to be
-- consumed by pandoc anyway. So instead, I am just planning on keeping
-- my own module parsing raw xml tags (for now).
import Data.List (find, intersperse) import Data.List (find, intersperse)
import Data.List.Split (splitOn) import Data.List.Split (splitOn)
import qualified System.Console.ANSI as AN import qualified System.Console.ANSI as AN
@ -25,9 +36,9 @@ data PotentiallyColorizedString = PCS
removeParagraphTags :: [TS.TagTree String] -> [TS.TagTree String] removeParagraphTags :: [TS.TagTree String] -> [TS.TagTree String]
removeParagraphTags (TS.TagLeaf (TS.TagClose "para") : TS.TagLeaf (TS.TagOpen "para" []) : rest) = removeParagraphTags (TS.TagLeaf (TS.TagClose "para") : TS.TagLeaf (TS.TagOpen "para" []) : rest) =
TS.TagLeaf (TS.TagText "\n") : removeParagraphTags rest TS.TagLeaf (TS.TagText "\n") : removeParagraphTags rest
-- In this case, it will be directly followed by a <para> branch -- In this case, it will be directly followed by a <para> branch
removeParagraphTags (TS.TagLeaf (TS.TagClose "para") : rest) = removeParagraphTags rest removeParagraphTags (TS.TagLeaf (TS.TagClose "para") : rest) = removeParagraphTags rest
-- In this case, it is directly behind by a <para> branch -- In this case, it is directly behind by a <para> branch
removeParagraphTags (TS.TagLeaf (TS.TagOpen "para" _) : rest) = removeParagraphTags rest removeParagraphTags (TS.TagLeaf (TS.TagOpen "para" _) : rest) = removeParagraphTags rest
removeParagraphTags (x : y : rest) = x : removeParagraphTags (y : rest) removeParagraphTags (x : y : rest) = x : removeParagraphTags (y : rest)
removeParagraphTags x = x removeParagraphTags x = x
@ -53,7 +64,6 @@ wrapColor c = wrapSGR (AN.SetColor AN.Foreground AN.Vivid c)
bold :: AN.SGR bold :: AN.SGR
bold = AN.SetConsoleIntensity AN.BoldIntensity bold = AN.SetConsoleIntensity AN.BoldIntensity
-- Replace tags with their PCS string equivalent. -- Replace tags with their PCS string equivalent.
replaceTagColor :: TS.TagTree String -> PotentiallyColorizedString replaceTagColor :: TS.TagTree String -> PotentiallyColorizedString
replaceTagColor (TS.TagLeaf (TS.TagText s)) = replaceTagColor (TS.TagLeaf (TS.TagText s)) =
@ -64,7 +74,7 @@ replaceTagColor (TS.TagLeaf (TS.TagText s)) =
replaceTagColor (TS.TagBranch "para" _ inner) = replaceTagColor (TS.TagBranch "para" _ inner) =
PCS PCS
{ colorized = mapM_ (colorized . replaceTagColor) inner, { colorized = mapM_ (colorized . replaceTagColor) inner,
nonColorized = concat $ map (nonColorized . replaceTagColor) inner nonColorized = concatMap (nonColorized . replaceTagColor) inner
} }
replaceTagColor (TS.TagBranch "code" _ [TS.TagLeaf (TS.TagText content)]) = replaceTagColor (TS.TagBranch "code" _ [TS.TagLeaf (TS.TagText content)]) =
PCS PCS
@ -150,13 +160,12 @@ replaceTagColor (TS.TagBranch "citerefentry" _ content) =
volumNum = case find (tagBranchTagMatches "manvolnum") content of volumNum = case find (tagBranchTagMatches "manvolnum") content of
Just (TS.TagBranch _ _ [TS.TagLeaf (TS.TagText str)]) -> Just str Just (TS.TagBranch _ _ [TS.TagLeaf (TS.TagText str)]) -> Just str
_ -> Nothing _ -> Nothing
combinedLink :: String combinedLink :: String
combinedLink = case (title, volumNum) of combinedLink = case (title, volumNum) of
(Just t, Just vn) -> concat [t, "(", vn, ")"] (Just t, Just vn) -> concat [t, "(", vn, ")"]
(Just t, Nothing) -> t (Just t, Nothing) -> t
_ -> "???" _ -> "???"
replaceTagColor unknown = replaceTagColor unknown =
PCS PCS
{ colorized = wrapColor AN.Red $ TS.renderTree [unknown], { colorized = wrapColor AN.Red $ TS.renderTree [unknown],

View File

@ -1,4 +1,4 @@
{ pkgs, lib, home-manager, defaultManualPath, system, json2nix, xmldoc2txt, ... { pkgs, lib, home-manager, defaultManualPath, system, json2nix, docbook2txt, ...
}: }:
let let
usage = pkgs.writeText "home-manager-search-usage" '' usage = pkgs.writeText "home-manager-search-usage" ''
@ -45,8 +45,8 @@ let
let let
# TODO: Color management here needs a refactoring badly... # TODO: Color management here needs a refactoring badly...
colorSuffix = if isColorized then "-color" else ""; colorSuffix = if isColorized then "-color" else "";
batColorArg = if isColorized then "--color=always " else ""; batColorArg = if isColorized then "-f " else "";
xmldoc2textColorArg = if isColorized then "-C " else ""; docbook2txtColorArg = if isColorized then "-C " else "";
template = if isColorized then optionTemplateColor else optionTemplate; template = if isColorized then optionTemplateColor else optionTemplate;
in pkgs.writers.writeBash in pkgs.writers.writeBash
"preview-home-manager-attrs-gomplate${colorSuffix}" '' "preview-home-manager-attrs-gomplate${colorSuffix}" ''
@ -54,7 +54,7 @@ let
JSON_MANUAL_PATH=$2 JSON_MANUAL_PATH=$2
JSON_DATA=$(${jq} ".\"$OPTION_KEY\"" $JSON_MANUAL_PATH) JSON_DATA=$(${jq} ".\"$OPTION_KEY\"" $JSON_MANUAL_PATH)
export DESCRIPTION=$(echo $JSON_DATA | ${jq} -r ".description" | ${xmldoc2txt}/bin/xmldoc2txt ${xmldoc2textColorArg}) export DESCRIPTION=$(echo $JSON_DATA | ${jq} -r ".description" | ${docbook2txt}/bin/docbook2txt ${docbook2txtColorArg})
EXAMPLE_DATA=$(echo $JSON_DATA | ${jq} -r ".example.text" 2>/dev/null | ${nixfmt}) EXAMPLE_DATA=$(echo $JSON_DATA | ${jq} -r ".example.text" 2>/dev/null | ${nixfmt})
if [ $? != 0 ]; then if [ $? != 0 ]; then

View File

@ -1,5 +1,5 @@
# TODO: # TODO:
{ pkgs, lib, nixpkgs, defaultManualPath, system, json2nix, xmldoc2txt, ... }: { pkgs, lib, nixpkgs, defaultManualPath, system, json2nix, docbook2txt, ... }:
let let
usage = pkgs.writeText "nix-option-search-usage" '' usage = pkgs.writeText "nix-option-search-usage" ''
Usage: Usage:
@ -46,7 +46,7 @@ let
# TODO: Color management here needs a refactoring badly... # TODO: Color management here needs a refactoring badly...
colorSuffix = if isColorized then "-color" else ""; colorSuffix = if isColorized then "-color" else "";
batColorArg = if isColorized then "--color=always " else ""; batColorArg = if isColorized then "--color=always " else "";
xmldoc2textColorArg = if isColorized then "-C " else ""; docbook2txtColorArg = if isColorized then "-C " else "";
template = if isColorized then optionTemplateColor else optionTemplate; template = if isColorized then optionTemplateColor else optionTemplate;
in pkgs.writers.writeBash in pkgs.writers.writeBash
"preview-nix-option-attrs-gomplate${colorSuffix}" '' "preview-nix-option-attrs-gomplate${colorSuffix}" ''
@ -54,7 +54,7 @@ let
JSON_MANUAL_PATH=$2 JSON_MANUAL_PATH=$2
JSON_DATA=$(${jq} ".\"$OPTION_KEY\"" $JSON_MANUAL_PATH) JSON_DATA=$(${jq} ".\"$OPTION_KEY\"" $JSON_MANUAL_PATH)
export DESCRIPTION=$(echo $JSON_DATA | ${jq} -r ".description" | ${xmldoc2txt}/bin/xmldoc2txt ${xmldoc2textColorArg}) export DESCRIPTION=$(echo $JSON_DATA | ${jq} -r ".description" | ${docbook2txt}/bin/docbook2txt ${docbook2txtColorArg})
EXAMPLE_DATA=$(echo $JSON_DATA | ${jq} -r ".example.text" 2>/dev/null) EXAMPLE_DATA=$(echo $JSON_DATA | ${jq} -r ".example.text" 2>/dev/null)
if [ $? != 0 ]; then if [ $? != 0 ]; then