LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-01-2017, 04:08 AM   #1
Michael Uplawski
Member
 
Registered: Dec 2015
Location: Normandy, France
Distribution: Debian buster/sid
Posts: 538
Blog Entries: 18

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
Script to provide missing header-fields in man-pages


I do not ask a question, here! (Just in case...)

In my newest blog-entry, I describe a way to produce man-pages from “Restructured Text” (docutils).

Part of my procedure is a Ruby-script, which modifies an existing man-page and provides the missing creation-date, version- and section-information.

As I do not ask a question, here (just in case...), all I say is that the routine is not complicated and probably easily reproduced in any other programming language.

Now, do as you please...

But if, by your own reflection, you arrive at something potentially useful, maybe either present it here or add a comment to my blog-post...

Cheerio.

[EDIT]: I just add the script, here, but its arguments are provided by a shell script. See blog.
Code:
#!/usr/bin/env ruby
#encoding: UTF-8

# Adds the creation date, section number, version and a category to a man-page
# or updates these data, as Docutils do not provide these values.

require 'filemagic'
require 'date'

# find the file-type for a given file name
def file_type(file)
  fm = FileMagic.fm
  file_magic = fm.file(file)
  fm.flags = [:mime_type]
  file_mime = fm.file(file)	
  return file_magic, file_mime
end

section = version = category = ''

if ARGV.empty?
  puts "No man-page file given!"
  exit false
elsif !File.exist?(ARGV[0] )
  puts "File %s does not exist" %ARGV[0]
  exit false
elsif !File.writable?(ARGV[0] )
  puts "File %s cannot be modified" %ARGV[0]
  exit false
else
  fname = ARGV[0]
  if ARGV.length > 1
    section = ARGV[1]
  end
  if ARGV.length > 2
    version = ARGV[2]
  end
  if ARGV.length > 3
    category = ARGV[3]
  end
  file = File.new(fname, 'r+')
  ftype = file_type(fname)
  if (ftype[0].include?('troff'))
    line = ''
    out = ''
    begin
      # read file line by line
      while file.gets 
        line = $_
        # anything but the sought header
        if '.' != line && !line.empty? && !line.start_with?('.TH')
          out << line
        else
          if line && !line.empty?
            # cut off every field.
            out << line.match(/.TH [^"]*/)[0]
            out << " \"%s\" \"%s\" \"%s\" \"%s\"\n" %[section, Date.today.strftime('%a %d %b %Y'), version, category]
          end
        end
      end
    rescue EOFError => ex
      puts "No header found!"
      exit 0
    end

    file.pos = 0
    file << out
    file.close
  else
    puts '(' << $0 << ") The file '%s' does not look like a man-page (%s)!\nDoing nothing!" %[fname, ftype[0]]
    exit 0
  end
end

Last edited by Michael Uplawski; 08-01-2017 at 04:27 AM. Reason: superficial smiley, explanations
 
Old 08-01-2017, 09:28 PM   #2
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 4,737

Rep: Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877
bookmarked
 
  


Reply

Tags
docutils, man-pages, restructured text, ruby


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Missing Man pages ctighe Linux - Newbie 3 02-09-2009 09:27 AM
Man pages ? Missing compaq_user7 DamnSmallLinux 5 07-30-2007 11:01 AM
Missing man pages alaios Linux - General 1 08-24-2005 11:13 PM
Missing man pages KissDaFeetOfSean Linux - Newbie 5 06-14-2005 05:43 AM
Missing man pages on Mandrake 10.1 jxu158 Mandriva 1 02-22-2005 02:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration