diff --git a/flake.nix b/flake.nix index 02ddc4c..06822c6 100644 --- a/flake.nix +++ b/flake.nix @@ -25,9 +25,9 @@ type = "app"; program = "${self.packages.${system}.nix2json}/bin/nix2json"; }; - xmldoc2txt = { + docbook2txt = { type = "app"; - program = "${self.packages.${system}.xmldoc2txt}/bin/xmldoc2txt"; + program = "${self.packages.${system}.docbook2txt}/bin/docbook2txt"; }; }; @@ -40,14 +40,14 @@ home-manager-search = pkgs.callPackage ./searchers/home-manager-search.nix { inherit home-manager; - inherit (self.packages.${system}) json2nix xmldoc2txt; + inherit (self.packages.${system}) json2nix docbook2txt; defaultManualPath = let pkg = self.packages.${system}.home-manager-json; in "${pkg}/share/doc/home-manager/options.json"; }; nix-option-search = pkgs.callPackage ./searchers/nix-option-search.nix { inherit nixpkgs; - inherit (self.packages.${system}) json2nix xmldoc2txt; + inherit (self.packages.${system}) json2nix docbook2txt; defaultManualPath = let pkg = self.packages.${system}.nix-options-json; in "${pkg}/share/doc/nixos/options.json"; @@ -67,8 +67,8 @@ # Internal Tools json2nix = pkgs.callPackage ./internals/json2nix { compiler = "ghc924"; }; - xmldoc2txt = - pkgs.callPackage ./internals/xmldoc2txt { compiler = "ghc924"; }; + docbook2txt = + pkgs.callPackage ./internals/docbook2txt { compiler = "ghc924"; }; }; overlays.default = _: prev: prev // self.packages.${system}; diff --git a/internals/xmldoc2txt/default.nix b/internals/docbook2txt/default.nix similarity index 57% rename from internals/xmldoc2txt/default.nix rename to internals/docbook2txt/default.nix index 793439b..53db948 100644 --- a/internals/xmldoc2txt/default.nix +++ b/internals/docbook2txt/default.nix @@ -1,4 +1,4 @@ { pkgs, compiler ? "ghc924", ... }: -pkgs.writers.writeHaskellBin "xmldoc2txt" { +pkgs.writers.writeHaskellBin "docbook2txt" { libraries = with pkgs.haskellPackages; [ tagsoup ansi-terminal split text ]; -} (builtins.readFile ./xmldoc2txt.hs) +} (builtins.readFile ./docbook2txt.hs) diff --git a/internals/xmldoc2txt/xmldoc2txt.hs b/internals/docbook2txt/docbook2txt.hs similarity index 88% rename from internals/xmldoc2txt/xmldoc2txt.hs rename to internals/docbook2txt/docbook2txt.hs index 7cd1a35..1ef19eb 100644 --- a/internals/xmldoc2txt/xmldoc2txt.hs +++ b/internals/docbook2txt/docbook2txt.hs @@ -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.Split (splitOn) import qualified System.Console.ANSI as AN @@ -25,9 +36,9 @@ data PotentiallyColorizedString = PCS removeParagraphTags :: [TS.TagTree String] -> [TS.TagTree String] removeParagraphTags (TS.TagLeaf (TS.TagClose "para") : TS.TagLeaf (TS.TagOpen "para" []) : rest) = TS.TagLeaf (TS.TagText "\n") : removeParagraphTags rest - -- In this case, it will be directly followed by a branch +-- In this case, it will be directly followed by a branch removeParagraphTags (TS.TagLeaf (TS.TagClose "para") : rest) = removeParagraphTags rest - -- In this case, it is directly behind by a branch +-- In this case, it is directly behind by a branch removeParagraphTags (TS.TagLeaf (TS.TagOpen "para" _) : rest) = removeParagraphTags rest removeParagraphTags (x : y : rest) = x : removeParagraphTags (y : rest) removeParagraphTags x = x @@ -53,7 +64,6 @@ wrapColor c = wrapSGR (AN.SetColor AN.Foreground AN.Vivid c) bold :: AN.SGR bold = AN.SetConsoleIntensity AN.BoldIntensity - -- Replace tags with their PCS string equivalent. replaceTagColor :: TS.TagTree String -> PotentiallyColorizedString replaceTagColor (TS.TagLeaf (TS.TagText s)) = @@ -64,7 +74,7 @@ replaceTagColor (TS.TagLeaf (TS.TagText s)) = replaceTagColor (TS.TagBranch "para" _ inner) = PCS { 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)]) = PCS @@ -150,13 +160,12 @@ replaceTagColor (TS.TagBranch "citerefentry" _ content) = volumNum = case find (tagBranchTagMatches "manvolnum") content of Just (TS.TagBranch _ _ [TS.TagLeaf (TS.TagText str)]) -> Just str _ -> Nothing - + combinedLink :: String combinedLink = case (title, volumNum) of (Just t, Just vn) -> concat [t, "(", vn, ")"] (Just t, Nothing) -> t - _ -> "???" - + _ -> "???" replaceTagColor unknown = PCS { colorized = wrapColor AN.Red $ TS.renderTree [unknown], diff --git a/searchers/home-manager-search.nix b/searchers/home-manager-search.nix index 61fd594..a49c2aa 100644 --- a/searchers/home-manager-search.nix +++ b/searchers/home-manager-search.nix @@ -1,4 +1,4 @@ -{ pkgs, lib, home-manager, defaultManualPath, system, json2nix, xmldoc2txt, ... +{ pkgs, lib, home-manager, defaultManualPath, system, json2nix, docbook2txt, ... }: let usage = pkgs.writeText "home-manager-search-usage" '' @@ -45,8 +45,8 @@ let let # TODO: Color management here needs a refactoring badly... colorSuffix = if isColorized then "-color" else ""; - batColorArg = if isColorized then "--color=always " else ""; - xmldoc2textColorArg = if isColorized then "-C " else ""; + batColorArg = if isColorized then "-f " else ""; + docbook2txtColorArg = if isColorized then "-C " else ""; template = if isColorized then optionTemplateColor else optionTemplate; in pkgs.writers.writeBash "preview-home-manager-attrs-gomplate${colorSuffix}" '' @@ -54,7 +54,7 @@ let JSON_MANUAL_PATH=$2 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}) if [ $? != 0 ]; then diff --git a/searchers/nix-option-search.nix b/searchers/nix-option-search.nix index e644150..d584f84 100644 --- a/searchers/nix-option-search.nix +++ b/searchers/nix-option-search.nix @@ -1,5 +1,5 @@ # TODO: -{ pkgs, lib, nixpkgs, defaultManualPath, system, json2nix, xmldoc2txt, ... }: +{ pkgs, lib, nixpkgs, defaultManualPath, system, json2nix, docbook2txt, ... }: let usage = pkgs.writeText "nix-option-search-usage" '' Usage: @@ -46,7 +46,7 @@ let # TODO: Color management here needs a refactoring badly... colorSuffix = if isColorized then "-color" 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; in pkgs.writers.writeBash "preview-nix-option-attrs-gomplate${colorSuffix}" '' @@ -54,7 +54,7 @@ let JSON_MANUAL_PATH=$2 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) if [ $? != 0 ]; then