Created PDF viewer framework

This commit is contained in:
isaacST08 2025-01-30 17:15:00 -07:00
commit 9524d93bd2
5 changed files with 258 additions and 0 deletions

View file

@ -0,0 +1,6 @@
{ ... }:
{
imports = [
./okular.nix
];
}

View file

@ -0,0 +1,42 @@
{
pkgs,
lib,
...
} @ moduleInheritencePackage: let
# The name of the pdf viewer
name = "okular";
# The viewer template
template = import ./viewerTemplate.nix;
inherit (lib.options) mkOption mkEnableOption;
inherit (lib.types) package str listOf;
in (
template {
inherit name moduleInheritencePackage;
options = {
enable = mkEnableOption "enable okular as the pdf file previewer.";
package = mkOption {
type = package;
default = pkgs.okular;
description = "okular package";
};
executable = mkOption {
type = str;
default = "okular";
description = "The executable name to call the viewer.";
};
args = mkOption {
type = listOf str;
default = [ "--unique" "file:%p#src:%l%f" ];
description = "Arguments to pass to the viewer.";
};
};
args = viewerCfg: ( viewerCfg.args );
}
)

View file

@ -0,0 +1,68 @@
# This function acts as a template for creating new pdf viewers.
# It enforces providing all the parameters required for creating
# a new pdf viewer for it to be able to work in the existing code.
#
# The first layer requirements are as follows:
{
# This is the name of the pdf viewer, it will only be used internally and
# MUST match the <name>.nix file that the pdf viewer is implemented in.
name,
#
# Module attribute set. This is the attribute set that the module that is
# defining a pdf viewer is passed as its input.
moduleInheritencePackage,
#
# These are the standard options for the pdf viewer just like creating any
# other module. Some options are required and are described below but
# it will also accept any other options that are provided to it.
options,
#
# These are the command line arguments that will accompany the executable
# when the view command is called.
# This is a function that will take in the cfg of its own pdf viewer.
# i.e. it will be called as "args cfg.pdfViewer.viewers.${name}"
args,
...
}: let
# Inherit the necessary variables available to any module.
inherit (moduleInheritencePackage) lib config;
#
# Inherit other useful functions.
inherit (lib.modules) mkIf;
#
# Set the cfg variable
cfg = config.vim.languages.tex;
#
# Set the cfg of the viewer itself
viewerCfg = cfg.pdfViewer.viewers.${name};
in {
# These are the options for the pdf viewer. It will accept any options
# provided to it but some options are mandatory:
options.vim.languages.tex.pdfViewer.viewers.${name} = ({
# The enable option. This one is self explanatory.
enable,
#
# This is the package option for the pdf viewer.
package,
#
# This is the executable that will be used to call the pdf viewer.
# It, along with package will result in:
# "<package_path>/bin/<executable>"
executable,
#
# Any other options provided are accepted.
...
} @ opts:
opts)
options;
# Check that the language, overall pdf viewing, and this pdf viewer have been enabled before making any
# config.
config = mkIf (cfg.enable && viewerCfg.enable) {
vim.languages.tex.pdfViewer = {
inherit name;
inherit (viewerCfg) package executable;
args = args viewerCfg;
};
};
}