Редагування сторінок підручника/wiki2man.py
Матеріал з docs.linux.org.ua — збірника документації з Unix/Linux українською мовою.
Переглядати та "вирізати" тільки з джерельного коду цієї сторінки!
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 20060223 wiki2man.py hse@ukr.net http://docs.linux.org.ua/dlou/index.php/Man
############################################################################
# Copyright © 2005 by hse #
# hse@ukr.net #
# #
# This program is free software; you can redistribute it and#or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the #
# Free Software Foundation, Inc., #
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
############################################################################
#
# Script convert wiki formated text to roff format used by man pages.
# You can chose encoding of output roff file to suite any
# GNU/Linux distributions and yours native language.
# For convenient usage use makeman.sh instead of running this script directly.
#
# version - 0.2.4
#
# ХАЙ UNIX ЗАГОВОРИТЬ УКРАЇНСЬКОЮ!!!
#
# For convenient usage use makeman.sh instead of running this script directly.
########## Read command line arguments ##########
# 1 - language;
# 2 - directory of wiki formated man page;
# 3 - output directory;
# 4 - name of file as you type to man command;
# 5 - man number 1-8;
# 6 - last modification time or version of wiki file;
# 7 - release version;
# 8 - list of manual page encoding "utf8 koi8-u ...".
import sys
lang=sys.argv[1]; DirIn=sys.argv[2]; DirOut=sys.argv[3]; File=sys.argv[4]; man=sys.argv[5]; date=sys.argv[6]; release=sys.argv[7]; manencoding=sys.argv[8:]
FileName=File+'.'+man; PathIn=DirIn+FileName
########## Define some functions ##########
def SH (i):
"""reformatting .SH"""
string = WorkList [i]; l = len(string)-2; r = 0
while string [0] == '=' and string [l] == '=':
WorkList [i] = string [1:l]
string = WorkList [i]; l=len(string)-1; r=r+1
if r == 2:
WorkList [i] = '\n.SH "'+string+'"\n.PP\n'
else:
WorkList [i] = '\n.SS "'+string+'"\n.PP\n'
def TP (i):
"""reformatting .TP"""
string = WorkList [i]
l=0; string1 = WorkList [i+l]
while string1[0] == ';':
j=0; finish=0; nexcl=1; s=0
while len(string) > j and finish == 0:
if string [j:j+8] == '<nowiki>':
nexcl=0; j=j+7
elif string [j:j+9] == '':
nexcl=1; j=j+8
elif string [j:j+4] == '<!--':
nexcl=0; j=j+3
elif string [j:j+3] == '-->':
nexcl=1; j=j+2
if string [j] == ':':
s=1
finish=nexcl*s; s=0; j=j+1
if len(string) == j:
WorkList [i] = '.TP\n.B '+string[1:]
elif string [j-1] == ':':
WorkList [i] = '.TP\n.B '+string[1:j-1]+'\n'+string[j:]
l=l+1; string1 = WorkList [i+l]
while string1[0] == ':':
WorkList [i+l] = '.br\n'+string1[1:]
l=l+1; string1 = WorkList [i+l]
########## Read wiki formated file ##########
FileIn=open(PathIn, 'r')
string='\n'
string=unicode(string, 'utf-8')
WorkList = [ string ]
# variable WorkList has list of elements of lines from wiki file
string='F'; i=0
while string!='':
string=FileIn.readline()
string=unicode(string, 'utf-8')
WorkList.append(string)
FileIn.close()
########## Reformatting from wiki to roff ##########
# TH:
string = WorkList[1]; title=date; version=date
if len(string) > 2 and string[0] != '=' and string[:4] != '<!--':
i=0
while len(string) > i and string[i] != '(':
i=i+1
title=string[:i-1]
if i <= len(string)-1:
version=string[i+1:len(string)-2]+' '+date
WorkList.pop(1)
WorkList.pop(0)
i=0; tabacc=-1; tab=0; tab2=0; col=0; nf=0; excl=0; nowiki=0
while len(WorkList) > i:
string = WorkList [i]
if len(string) > 1:
# reformatting "nowiki"
if string [:9] == '</nowiki>':
WorkList [i] = string[9:]
nowiki=0
if nowiki == 0:
# reformatting "pre"
if string [:6] == '':
WorkList [i] ='\n.fi\n'+string[6:] nf=0
# reformatting "tt" if string[:4] == '': WorkList[i]='\n.nf\n'+string[4:] nf=2 elif string[:5] == '': WorkList[i]='\n.fi\n'+string[5:] nf=0
# reformatting " " if string [0] == ' ': if nf == 0: nf=1; WorkList [i] ='\n.nf\n'+string elif nf == 1: WorkList [i] =string else: if nf == 1: nf=0; WorkList [i] = '\n.fi\n' WorkList.insert (i+1, string)
string = WorkList [i] if nf != 2 and nowiki == 0: # reformatting excluded text if excl == 1: WorkList[i] ='.\" '+string[0:] string = WorkList [i]
if nf == 0: # format titles if string [0] == '=' and string[len(string)-2] == '=': SH(i)
# format descriptions elif string [0] == ';': TP(i)
# format descriptions elif string [0] == ':': WorkList[i] = '.br\n '+string [1:];
# format descriptions elif string [0] == '*': WorkList[i] = '.br\n * '+string [1:];
# format tables 2 elif string [:2] == '{|': if tab2 > 0: WorkList[i] = '.RS\n'; tab2=tab2+1; col=0 else: WorkList[i] = ; tab2=1
elif string [:2] == '|-' and tab2 > 0: WorkList[i] = ; col=0
elif string [:2] == '|}': if tab2 == 1: WorkList[i] = '.SS\n'; col=0; tab2=0 elif tab2 > 1: WorkList[i] = '.RE\n'; col=0; tab2=tab2-1
elif string [:8] == '|valign=' and tab2 > 0: j=9 while len(string) > j and string[j]!='|': j=j+1 if string [j] == '|': if col == 0: WorkList[i]='.TP\n'+string[j+1:]; col=1 elif col > 0: WorkList[i]=string[j+1:]; col=2 elif col > 1: WorkList[i]='.PP\n'+string[j+1:]; col=col+1
elif string [:1] == '|' and tab2 > 0: if col == 0: WorkList[i]='.TP\n'+string[1:]; col=1 elif col == 1: WorkList[i]=string[1:]; col=col+1 elif col > 1: WorkList[i]='.PP\n'+string[1:]; col=col+1
# delete wiki "Category:" elif string [:11] == '[[Category:': WorkList [i] = #WorkList.pop(i) #break
string = WorkList[i]; j=0; B=-1; I=-1; U=-1; K=-1; K1=-1 while len(string) > j: # reformatting excluded text if string [j:j+4] == : string=string[:j]+'\n'+string[j+3:] excl=0; j=j-1
if excl == 0: # Change some symbols: — « » — © " & < > if string [j:j+8] == '—': string=string[:j]+unicode('—', 'utf-8')+string[j+8:] elif string [j:j+7] == '«': string=string[:j]+unicode('«', 'utf-8')+string[j+7:] elif string [j:j+7] == '»': string=string[:j]+unicode('»', 'utf-8')+string[j+7:] elif string [j:j+7] == '—': string=string[:j]+unicode('—', 'utf-8')+string[j+7:] elif string [j:j+6] == '©': string=string[:j]+unicode('©', 'utf-8')+string[j+6:] elif string [j:j+6] == '"': string=string[:j]+unicode('"', 'utf-8')+string[j+6:] elif string [j:j+6] == ' ': string=string[:j]+unicode(' ', 'utf-8')+string[j+6:] elif string [j:j+5] == '&': string=string[:j]+unicode('&', 'utf-8')+string[j+5:] elif string [j:j+4] == '<': string=string[:j]+unicode('<', 'utf-8')+string[j+4:] elif string [j:j+4] == '>': string=string[:j]+unicode('>', 'utf-8')+string[j+4:]
# reformatting "-" or "\" elif string[j:j+1] == '-': string=string[0:j] + '\\' + string[j:] j=j+1 elif string[j:j+1] == '\\': string=string[0:j] + '\e' + string[j+1:] j=j+1
# reformatting "nowiki" elif string [j:j+8] == '': nowiki=1 if nf != 2: string=string[:j]+string[j+8:] j=j elif string [j:j+9] == '': nowiki=0 if nf != 2: string=string[:j]+string[j+9:] j=j
if nowiki == 0: if string[j:j+5] == "": if B != -1 and I == -1 : if tabacc == 1: string=string[:B]+'"'+string[B+3:j]+'"'+string[j+3:] j=j-4; B=-1 else: string=string[:B]+'\\fB'+string[B+3:j]+'\\fR'+string[j+3:] j=j+1; B=-1 if I != -1 and B == -1: string=string[:I]+'\\fI'+string[I+2:j]+'\\fR'+string[j+2:] j=j+2; I=-1
# reformatting boolean text 1 elif string[j:j+3] == "": if B == -1: B=j else: if tabacc == 1: string=string[:B]+'"'+string[B+3:j]+'"'+string[j+3:] j=j-4; B=-1 elif j+3-B > 5: string=string[:B]+'\\fB'+string[B+3:j]+'\\fR'+string[j+3:] j=j+1; B=-1
# reformatting italic text 1 elif string[j:j+2] == "" and B == -1: if I == -1: I=j else: if j+3-I > 2: string=string[:I]+'\\fI'+string[I+2:j]+'\\fR'+string[j+2:] j=j+2; I=-1
# reformatting "pre"
elif string [j:j+5] == '': string=string[:j]+'\n.nf\n'+string[j+5:] nf=2; j=j+3 elif string [j:j+6] == '':
string=string[:j]+'\n.fi\n'+string[j+6:] nf=0; j=j+3
# reformatting "code"
elif string [j:j+6] == '':
string=string[:j]+'\n.nf\n'+string[j+6:]
nf=2; j=j+3
elif string [j:j+7] == '':
string=string[:j]+'\n.fi\n'+string[j+7:]
nf=0; j=j+3
if nf == 0: # reformatting boolean text 2 if string[j:j+3] == '': string=string[:j]+'\\fB'+string[j+3:] j=j+2 elif string[j:j+4] == '': string=string[:j]+'\\fR'+string[j+4:] j=j+2
# reformatting italic text 2 elif string[j:j+3] == '': string=string[:j]+'\\fI'+string[j+3:] j=j+2 elif string[j:j+4] == '': string=string[:j]+'\\fR'+string[j+4:] j=j+2
# format undelined text elif string[j:j+3] == '': U=j
elif string[j:j+4] == '' and U != -1: string=string[:U]+'\\fB\\fI'+string[U+3:j]+'\\fB\\fR'+string[j+4:] j=j+7; U=-1
# brack line 1
elif string[j:j+4] == '
':
string=string[0:j] + '\n.br ' + string[j+4:]; j=j+2
# brack line 2
elif string[j:j+6] == '
':
string=string[0:j] + '\n.PP\n' + string[j+6:]; j=j+2
# format tables 1
elif string [j:j+6] == '| j and string[j]!='>':
j=j+1 if string [j] == '>': tabacc=tabacc+1 if tabacc == 1: string=string[:Ktab]+string[j+1:] j=Ktab-1 else: string=string[:Ktab]+'\n.PP\n'+string[j+1:] j=Ktab+3 else: j=Ktab elif string [j:j+4] == ' | j and string[j]!='>':
j=j+1
if string [j] == '>':
string=string[:Ktab]+string[j+1:]
j=Ktab-1
else:
j=Ktab
elif string [j:j+7] == '
j=j+1 if string [j] == '>': string=string[:tab]+string[j+1:] j=tab-1; tab=0 else: j=tab; tab=1
# format tabel 2 {| |- | || |} elif string [j:j+2] == '||' and tab2 > 0 and col > 0: string=string[:j]+'\n'+string[j+2:]; col=col+1
# format div ????
elif string [j:j+4] == 'j=j+1 if string [j] == '>': string=string[:div]+string[j+1:] j=div-1 else: j=div
elif string [j:j+5] == 'j=j+1 if string [j] == '>': string=string[:div]+string[j+1:] j=div-1 else: j=div
# format internal links elif string[j:j+2] == '[[': K=j elif string[j] == '|': if K != -1: K1=j elif string[j:j+2] == ']]': if K != -1 and K1 != -1: string=string[:K]+string[K1+1:j]+string[j+2:] j=j-K1+K-2; K=-1; K1=-1 elif K != -1 and K1 == -1: string=string[:K]+string[K+2:j]+string[j+2:] j=j-4; K=-1 j=j+1 WorkList[i]= string i=i+1
- Make title
string='.\" '+unicode('©', 'utf-8')+' 2005 DLOU docs@linux.org.ua\n.\" URL http://docs.linux.org.ua/dlou/index.php/Man\n.\"\n.\" Permission is granted to copy, distribute and/or modify this document\n.\" under the terms of the GNU Free Documentation License, Version 1.2\n.\" or any later version published by the Free Software Foundation;\n.\" with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. \n.\"\n.\" A copy of the license is included as a file called COPYING in the\n.\" main directory of the manpeges-* source package.\n.\"\n.\" This manpage has been automatically generated by wiki2man.py\n.\" This tool can be found at:\n.\" <http://docs.linux.org.ua/dlou/index.php/'+unicode('Редагування сторінок підручника', 'utf-8')+'#wiki_to_man>\n.\" Please send any bug reports, improvements, comments, patches, etc. to <hse@ukr.net>.\n\n.TH "'+File+'" "'+man+'" "'+version+'" "'+unicode('©', 'utf-8')+' 2006 DLOU, GNU FDL" "'+title+'"\n' string=string.encode('utf-8') string=unicode(string, 'utf-8') WorkList.insert(0,string)
- Output roff formatted file ##########
- Output Unicode cod to standart out:
- print WorkList[0:]
- Output encoded symbols:
string= for i in range(len(WorkList)): string =string + WorkList[i]
- Delete empty lines and some think else..., just for making roff code beter:
i=0 while len(string) > i: if string[i:i+6] == '\n.SS \n': string=string[:i+5]+string[i+6:] if string[i:i+5] == '\n\n\n\n\n': string=string[:i]+string[i+3:] if string[i:i+4] == '\n\n\n\n': string=string[:i]+string[i+2:] if string[i:i+3] == '\n\n\n': string=string[:i]+string[i+1:] if string[i:i+2] == '\n\n': string=string[:i]+string[i+0:] if string[i:i+2] == '\n\n': string=string[:i]+string[i+0:] i=i+1
for i in manencoding: # Output to standart out: #print string.encode(i)
# Output to file: PathOut=DirOut+'/manpages-'+lang+'-'+i+'-'+release+'/man'+man+'/'+FileName FileOut = open(PathOut, 'w') StringOut = string.encode(i) FileOut.write(StringOut) FileOut.close()

