Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[HOWTO] Convert man page to pdf while reading it (urxvt)
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
frankenputer
n00b
n00b


Joined: 09 Mar 2016
Posts: 26

PostPosted: Sat Jul 02, 2016 9:24 am    Post subject: [HOWTO] Convert man page to pdf while reading it (urxvt) Reply with quote

Hello,

I've been converting man pages by hand for a long time,
and since I gained more knowledge in writing extensions for urxvt I ported my shell code
to make it easy for anyone wanting to convert man page while reading it. With the shell alternative
I had to quit reading the man page or open another instance/tab to convert the man page by hand,
but with this extension the conversion is super easy.

[img]https://cloud.githubusercontent.com/assets/4725121/16555529/6e02ec1c-41d5-11e6-9ca6-1009de85e11a.png[/img]

Now I can enjoy reading the rest of gcc(1), lol.

Filename: man

Code:
#! /usr/bin/env perl
# Author:   Aaron Caffrey
# Website:  https://github.com/wifiextender/urxvt-man2pdf
# License:  GPLv3

# Usage: put the following lines in your .Xdefaults/.Xresources:
# URxvt.perl-ext-common           : man
# URxvt.keysym.Control-Shift-X    : perl:man:topdf
use strict;
use warnings;

sub on_user_command {
  my ($self, $cmd) = @_;

  if ($cmd eq "man:topdf") {
    my $cur_row = $self->nrow - 1;
    my $top = $self->top_row;

    while ($cur_row >= $top) {
      my $line_obj = $self->line ($cur_row);
      my $it_found_smth = $self->try_conversion ($line_obj->t);

      last if ($it_found_smth);
      $cur_row = $line_obj->beg - 1;
    }
  }
  ()
}

sub try_conversion {
  my ($self) = shift;
  my ($text) = @_;

  if ($text =~ /\([^)]*\)/) {
    $text =~ s/\([^)]*\)//g;         # strip (1) from printf(1)
    $text =~ s/(?!-)[[:punct:]]//g;  # strip [\$#@~!&*()\[\];.,:?^`\\\/]+;
    my @arr = split(/\s+/, $text);
    my $page = $arr[$#arr] ? lc $arr[$#arr] : "";

    # the LESS pager line makes it easy for us
    if ($page =~ /\d+$/) {
      my @new_arr = split(' ', join(' ', @arr)); # strip left-right space
      if (lc $new_arr[0] eq "manual" and lc $new_arr[3] eq "line") {
        $page = lc $new_arr[2];
      }
    }

    if ($page ne "") {
      my $has_ext = `man -Iw $page`;  # does the match has some file extension

      if ($? == 0) {
        if ($has_ext =~ /\.\w+$/) {
          $self->exec_async ("man -Tpdf $page > /tmp/$page.pdf");
          $self->exec_async ("notify-send \"Trying to convert /tmp/$page.pdf\"");
          return 1;
        }
      }
    }
  }
  return 0;
}

# getpwuid can traverse /proc really fast
# `pidof -s man` =~ /\d+/ is neat
# but all of the above fails when the
# man page is so short that `man'
# exits and closes the pid


Cheers

Edited the post to sync the code
Back to top
View user's profile Send private message
patter
n00b
n00b


Joined: 01 Aug 2016
Posts: 3

PostPosted: Mon Aug 01, 2016 1:08 pm    Post subject: Reply with quote

This is a rather wonderful script & I don't want to rain on your parade, but man can write PDFs directly

Code:
man -t pdf [topic] > foo.pdf
Back to top
View user's profile Send private message
shrike
Tux's lil' helper
Tux's lil' helper


Joined: 20 Feb 2004
Posts: 114
Location: Closer to home

PostPosted: Wed Dec 28, 2016 1:46 pm    Post subject: Reply with quote

'man -t' seems the way to go but fails:

Code:

# man -t pdf man > man.pdf
No manual entry for pdf


This fails too:

Code:

# man -t man | ps2pdf –  > man.pdf
Error: /undefinedfilename in (\342\200\223)
Operand stack:

Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push
Dictionary stack:
   --dict:1186/1684(ro)(G)--   --dict:0/20(G)--   --dict:77/200(L)--
Current allocation mode is local
Last OS error: No such file or directory
GPL Ghostscript 9.15: Unrecoverable error, exit code 1


Are there other methods?
Back to top
View user's profile Send private message
ian.au
Guru
Guru


Joined: 07 Apr 2011
Posts: 387
Location: Australia

PostPosted: Wed Dec 28, 2016 8:56 pm    Post subject: Reply with quote

i.e.
Code:
man -t less > ~/less.pdf
works for me.
Back to top
View user's profile Send private message
shrike
Tux's lil' helper
Tux's lil' helper


Joined: 20 Feb 2004
Posts: 114
Location: Closer to home

PostPosted: Thu Dec 29, 2016 2:02 am    Post subject: Reply with quote

Cool!

I had to add postscript flag and it works:

Code:

 USE="postscript" emerge -a1v --changed-use app-text/qpdfview


thanks,

shrike
Back to top
View user's profile Send private message
Ant P.
Advocate
Advocate


Joined: 18 Apr 2009
Posts: 4439

PostPosted: Thu Dec 29, 2016 11:29 pm    Post subject: Reply with quote

I don't know about sys-apps/man, but man-db has the -H flag which opens the page right in your browser. It makes a huge mess of gcc(1) though.
_________________
*.ebuild // /etc/service/*
Back to top
View user's profile Send private message
helecho
Apprentice
Apprentice


Joined: 26 Dec 2016
Posts: 202

PostPosted: Sun Sep 10, 2017 6:26 am    Post subject: man man.1 Reply with quote

patter wrote:
This is a rather wonderful script & I don't want to rain on your parade, but man can write PDFs directly. [...]

man wrote:
−T[device], −−troff−device[=device]
This option is used to change groff (or possibly troff's) output to be suitable for a device other than the default. It implies −t.
Examples (provided with Groff-1.17) include dvi, latin1, ps, utf8, X75 and X100. (see also: groff(1))

Code:
man -Tpdf man.1 > man.pdf


The on-line manual is all we need! :wink:

N.B: The name of the mentioned software is sys-apps/man-db!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum