何かとローカルに置きたがるので、JM も。

実は前々からやりたかったのだが、html 化の為のスクリプトとかが 無かったので止まっていた。で、改めてディレクトリ配下を眺めていて、 最低限の、読める程度のものを生成してみた。

概要

JM Project の、 配布用アーカイブ から全体のアーカイブを一括ダウンロードできる。

この中にインストール用スクリプトなどはあるが、静的 html ページの 生成までは無い。

ディレクトリを眺めていて、方針を立てた

  • 変換は個々に行う。相互リンクは捨てる
  • ファイル名のみだと重複するので、ディレクトリ分けを踏襲する
  • 1 つの html ファイルに全リンクを置く

手順

準備

man2html コマンドを使うので、man2html-base パッケージをインストールしておく。

アーカイブの取得

2016/05/17 現在。毎月 15 日に更新されるそうなので、ファイル名は 毎月変わる。

$ wget http://linuxjm.osdn.jp/man-pages-ja-20160515.tar.gz
$ tar xvf man-pages-ja-20160515.tar.gz

man-pages-ja-20160515 ディレクトリが作成される。

静的 html の生成

以下のスクリプト(.rb として保存)を実行する。 事前に SRC_DIR に解凍してできたディレクトリ名を設定する

出力先は DST_DIR に設定する。ここでは html としている。

#! /usr/bin/env ruby
# encoding: utf-8
require "find"
require "pathname"
require "fileutils"

SRC_DIR = "man-pages-ja-20160515"
DST_DIR = "html"
TOPPAGE = DST_DIR + "/index.html"
MAN2HTML = "/usr/bin/man2html"

def conv( infile, outdir, fd )
  odir = Pathname.new( outdir )
  odir = odir.join( File.basename( File.dirname( File.dirname( infile ) ) ) )
  odir = odir.join( File.basename( File.dirname( infile ) ) )
  outfile = Pathname.new( odir )
  outfile = outfile.join( File.basename( infile ) )
  outfile = outfile.sub_ext( ".html" )

  if not File.directory? ( odir ) then
    FileUtils.mkdir_p( odir )
  end

  system( MAN2HTML + " " + infile + " > " + outfile.to_path )

  link = Pathname.new( "" )
  link = link.join( File.basename( File.dirname( File.dirname( infile ) ) ) )
  link = link.join( File.basename( File.dirname( infile ) ) )
  link = link.join( File.basename( infile ) )
  link = link.sub_ext( ".html" )
  tag = File.basename( infile )

  fd.puts( "<a href=\"" + link.to_path + "\"/>" + tag + "</a>" )
end

def heada( fd )
  fd.puts( "<html><body>" )
end
def foota( fd )
  fd.puts( "</body></html>" )
end

def main( argv )
  if not File.directory? ( DST_DIR ) then
    FileUtils.mkdir_p( DST_DIR )
  end

  fd = open( TOPPAGE, "w" )
  heada( fd )
  # man page ファイルのみ取り出す。 *.[1-9]
  Find.find( SRC_DIR ) do |f|
    if /\.[1-9]/.match( File.extname( f ) ) then
      conv( f, DST_DIR, fd )
    end
  end
  foota( fd )
  fd.close()
end
main ARGV

変換元ファイルは *.1 の様に「末尾(拡張子)が 1 桁の数字」で 識別している。

パスの先頭を削除するのに basename と dirname を冗長に、 しかも固定的に使っている。これはちょっと残念な点。

まぁ “/” で分割して再結合すれば良いのだが、そういう関数/メソッドが 見つからなかった。

ToDo

  • html としての宣言や head セクション。
  • サブカテゴリや man1 ~ man8 のセクションに分ける

html としての体裁は lint でエラーを出しながらやるのが簡単。 html5 の lint を設定したので、それでやるかな。

今は Find.find の出現順に、ファイル単位で処理している。 この順序は不定との事。なので、セクションに分けるには、 配列などにプールしてからソートして、 サブカテゴリやファイル末尾(.1 や .8)の切り替わりを 見るのが良さそう。