summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2018-06-25 17:13:06 +0200
committerPatrick Spek <p.spek@tyil.nl>2018-06-25 17:13:06 +0200
commitce75cb5f6df6fa075075bdb88d419c665e86e87f (patch)
tree89eb6da08a5f83229cdcd445cb5057d6ed692c80
parentda745e39c4e6d91e8ade1eac3d6d7e353dd22635 (diff)
downloadPod::To::Pager-ce75cb5f6df6fa075075bdb88d419c665e86e87f.tar.gz
Pod::To::Pager-ce75cb5f6df6fa075075bdb88d419c665e86e87f.tar.bz2
Resolve module names if target is not found as relative file
-rw-r--r--.gitignore6
-rw-r--r--CHANGELOG.md5
-rw-r--r--META6.json44
-rw-r--r--bin/p6man23
-rw-r--r--lib/Pod/To/Pager/Binary.pm658
-rw-r--r--lib/Pod/To/Pager/ModuleResolver.pm622
6 files changed, 119 insertions, 39 deletions
diff --git a/.gitignore b/.gitignore
index 5f2320e..27c54eb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,5 +2,7 @@
.precomp
# Editor files
-*~ # emacs
-.*.sw? # vim
+*~
+.*.sw?
+.idea/
+*.iml
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8f5f501..fb89507 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic
Versioning](http://semver.org/spec/v2.0.0.html).
+## [UNRELEASED]
+### Added
+- Giving `p6man` the name of an installed module will attempt to look it up and
+ render the pod document embedded inside it.
+
## [0.2.0] - 2018-06-24
### Added
- Add pod structure to `p6man`
diff --git a/META6.json b/META6.json
index 9e03bd2..ec3fee0 100644
--- a/META6.json
+++ b/META6.json
@@ -1,26 +1,20 @@
{
- "authors": [
- "Patrick Spek <p.spek@tyil.work>"
- ],
- "depends": [
- "Terminal::ANSIColor"
- ],
- "description": "Nondescript",
- "license": "AGPL-3.0",
- "meta-version": 0,
- "name": "Pod::To::Pager",
- "perl": "6.c",
- "provides": {
- "Pod::To::Pager": "lib/Pod/To/Pager.pm6",
- "Pod::To::Pager::BorderedBlock": "lib/Pod/To/Pager/BorderedBlock.pm6",
- "Pod::To::Pager::Handlers": "lib/Pod/To/Pager/Handlers.pm6",
- "p6man": "bin/p6man"
- },
- "resources": [
-
- ],
- "tags": [
-
- ],
- "version": "0.2.0"
-} \ No newline at end of file
+ "license": "AGPL-3.0",
+ "depends": ["Terminal::ANSIColor"],
+ "provides": {
+ "p6man": "bin/p6man",
+ "Pod::To::Pager::Handlers": "lib/Pod/To/Pager/Handlers.pm6",
+ "Pod::To::Pager": "lib/Pod/To/Pager.pm6",
+ "Pod::To::Pager::BorderedBlock": "lib/Pod/To/Pager/BorderedBlock.pm6",
+ "Pod::To::Pager::Binary": "lib/Pod/To/Pager/Binary.pm6",
+ "Pod::To::Pager::ModuleResolver": "lib/Pod/To/Pager/ModuleResolver.pm6"
+ },
+ "name": "Pod::To::Pager",
+ "description": "Nondescript",
+ "resources": [],
+ "perl": "6.c",
+ "meta-version": 0,
+ "version": "0.2.0",
+ "authors": ["Patrick Spek <p.spek@tyil.work>"],
+ "tags": []
+}
diff --git a/bin/p6man b/bin/p6man
index b5f8401..30ed0b2 100644
--- a/bin/p6man
+++ b/bin/p6man
@@ -2,23 +2,22 @@
use v6.c;
+use Pod::To::Pager::Binary;
+
#| Render a Perl 6 program's pod structure.
-sub MAIN (
- #| Path to the Perl 6 program or POD file.
- Str:D $file,
+multi sub MAIN (
+ #| Name of an installed Perl 6 module, or a path to the Perl 6 program or
+ #| POD file.
+ Str:D $document is copy,
#| Don't pipe output to a pager.
Bool:D :$skip-pager = False,
) {
- my Str $command = "'$*EXECUTABLE' --doc=Pager '$file'";
-
- if (!$skip-pager) {
- my Str $pager = %*ENV<PAGER> // ($*DISTRO.is-win ?? 'more' !! 'less -r');
-
- $command ~= " | $pager";
- }
-
- shell $command;
+ exit run-p6man(
+ $document,
+ :$skip-pager,
+ try-resolving => !($document.IO.e && $document.IO.f),
+ );
}
=begin pod
diff --git a/lib/Pod/To/Pager/Binary.pm6 b/lib/Pod/To/Pager/Binary.pm6
new file mode 100644
index 0000000..300d03f
--- /dev/null
+++ b/lib/Pod/To/Pager/Binary.pm6
@@ -0,0 +1,58 @@
+#! /usr/bin/env false
+
+use v6.c;
+
+use Pod::To::Pager::ModuleResolver;
+
+unit module Pod::To::Pager::Binary;
+
+#| Render a local file.
+multi sub run-p6man (
+ Str:D $path,
+ Bool:D :$skip-pager = False,
+ Bool:D :$try-resolving where !*,
+ --> Int
+) is export {
+ my Str $command = "'$*EXECUTABLE' --doc=Pager '$path'";
+
+ if (!$skip-pager) {
+ my Str $pager = %*ENV<PAGER> // ($*DISTRO.is-win ?? 'more' !! 'less -r');
+
+ $command ~= " | $pager";
+ }
+
+ shell $command;
+
+ exit 0;
+}
+
+#| Render an installed module.
+multi sub run-p6man (
+ Str:D $module,
+ Bool:D :$skip-pager = False,
+ Bool:D :$try-resolving where *,
+ --> Int
+) is export {
+ my Str $path = resolve-module($module);
+
+ # Make sure a result was found by resolve-module
+ if (!$path) {
+ note "No such module or file: $module";
+ return 2;
+ }
+
+ # Check for external pod
+ my @extensions = [
+ "pod",
+ "pod6",
+ ];
+
+ for @extensions {
+ my IO::Path:D $alternative = $path.IO.extension($_);
+
+ return run-p6man($alternative.absolute, :$skip-pager, :!try-resolving) if $alternative.e;
+ }
+
+ # Run p6man with the found path
+ run-p6man($path, :$skip-pager, :!try-resolving);
+}
diff --git a/lib/Pod/To/Pager/ModuleResolver.pm6 b/lib/Pod/To/Pager/ModuleResolver.pm6
new file mode 100644
index 0000000..be4e193
--- /dev/null
+++ b/lib/Pod/To/Pager/ModuleResolver.pm6
@@ -0,0 +1,22 @@
+#! /usr/bin/env false
+
+use v6.c;
+
+unit module Pod::To::Pager::ModuleResolver;
+
+#| Resolve a module name to the path it's installed to.
+sub resolve-module (
+ #| Name of the module.
+ Str:D $module,
+ --> Str
+) is export {
+ my Proc $zef = run << zef locate "$module" >>, :out;
+
+ for $zef.out.lines -> $line {
+ next unless $line ~~ / \S+ " => " (\S+) /;
+
+ return ~$0;
+ }
+
+ return "";
+}