1
0
mirror of https://github.com/h7x4/nix-attr-search.git synced 2024-12-21 11:47:28 +01:00

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";
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};

View File

@ -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)

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.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 <para> branch
-- In this case, it will be directly followed by a <para> branch
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 (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],

View File

@ -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

View File

@ -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