@ -13,7 +13,7 @@ from unidecode import unidecode
import locale
import locale
import re
import re
import sys
import sys
import warnings
#import warnings
from songbook . authors import processauthors
from songbook . authors import processauthors
from songbook . plastex import simpleparse
from songbook . plastex import simpleparse
@ -22,6 +22,7 @@ from songbook.plastex import simpleparse
keywordPattern = re . compile ( r " ^ % ( \ w+) \ s?(.*)$ " )
keywordPattern = re . compile ( r " ^ % ( \ w+) \ s?(.*)$ " )
firstLetterPattern = re . compile ( r " ^(?: \ { ? \\ \ w+ \ }?)*[^ \ w]*( \ w) " )
firstLetterPattern = re . compile ( r " ^(?: \ { ? \\ \ w+ \ }?)*[^ \ w]*( \ w) " )
def sortkey ( value ) :
def sortkey ( value ) :
'''
'''
From a title , return something usable for sorting . It handles locale ( but
From a title , return something usable for sorting . It handles locale ( but
@ -30,35 +31,40 @@ def sortkey(value):
'''
'''
return locale . strxfrm ( unidecode ( simpleparse ( value ) . replace ( ' ' , ' A ' ) ) )
return locale . strxfrm ( unidecode ( simpleparse ( value ) . replace ( ' ' , ' A ' ) ) )
def processSXDEntry ( tab ) :
def processSXDEntry ( tab ) :
return ( tab [ 0 ] , tab [ 1 ] , tab [ 2 ] )
return ( tab [ 0 ] , tab [ 1 ] , tab [ 2 ] )
def processSXD ( filename ) :
def processSXD ( filename ) :
file = open ( filename )
index_ file = open ( filename )
data = [ ]
data = [ ]
for line in file :
for line in index_ file:
data . append ( line . strip ( ) )
data . append ( line . strip ( ) )
file . close ( )
index_ file. close ( )
i = 1
i = 1
idx = index ( data [ 0 ] )
idx = index ( data [ 0 ] )
while len ( data ) > i and data [ i ] . startswith ( ' % ' ) :
while len ( data ) > i and data [ i ] . startswith ( ' % ' ) :
keywords = keywordPattern . match ( data [ i ] ) . groups ( )
keywords = keywordPattern . match ( data [ i ] ) . groups ( )
idx . keyword ( keywords [ 0 ] , keywords [ 1 ] )
idx . keyword ( keywords [ 0 ] , keywords [ 1 ] )
i + = 1
i + = 1
idx . compileKeywords ( )
idx . compileKeywords ( )
for i in range ( i , len ( data ) , 3 ) :
for i in range ( i , len ( data ) , 3 ) :
entry = processSXDEntry ( data [ i : i + 3 ] )
entry = processSXDEntry ( data [ i : i + 3 ] )
idx . add ( entry [ 0 ] , entry [ 1 ] , entry [ 2 ] )
idx . add ( entry [ 0 ] , entry [ 1 ] , entry [ 2 ] )
return idx
return idx
class index :
class index :
def __init__ ( self , indextype ) :
def __init__ ( self , indextype ) :
self . data = dict ( )
self . data = dict ( )
self . keywords = dict ( )
self . keywords = dict ( )
self . prefix_patterns = [ ]
self . authwords = { " after " : [ ] , " ignore " : [ ] , " sep " : [ ] }
if indextype == " TITLE INDEX DATA FILE " :
if indextype == " TITLE INDEX DATA FILE " :
self . indextype = " TITLE "
self . indextype = " TITLE "
elif indextype == " SCRIPTURE INDEX DATA FILE " :
elif indextype == " SCRIPTURE INDEX DATA FILE " :
@ -70,23 +76,21 @@ class index:
def filter ( self , key ) :
def filter ( self , key ) :
letter = firstLetterPattern . match ( key ) . group ( 1 )
letter = firstLetterPattern . match ( key ) . group ( 1 )
if re . match ( ' \ d ' , letter ) :
if re . match ( ' \ d ' , letter ) :
letter = ' 0-9 '
letter = ' 0-9 '
return ( letter . upper ( ) , key )
return ( letter . upper ( ) , key )
def keyword ( self , key , word ) :
def keyword ( self , key , word ) :
if not self . keywords . has_ key( key ) :
if not key in self . keywords . keys ( ) :
self . keywords [ key ] = [ ]
self . keywords [ key ] = [ ]
self . keywords [ key ] . append ( word )
self . keywords [ key ] . append ( word )
def compileKeywords ( self ) :
def compileKeywords ( self ) :
self . prefix_patterns = [ ]
if self . indextype == " TITLE " :
if self . indextype == " TITLE " :
if ' prefix ' in self . keywords :
if ' prefix ' in self . keywords :
for prefix in self . keywords [ ' prefix ' ] :
for prefix in self . keywords [ ' prefix ' ] :
self . prefix_patterns . append ( re . compile ( r " ^( %s )( \ b| \\ )( \ s*.*)$ " % prefix ) )
self . prefix_patterns . append ( re . compile ( r " ^( %s )( \ b| \\ )( \ s*.*)$ " % prefix ) )
self . authwords = { " after " : [ ] , " ignore " : [ ] , " sep " : [ ] }
if self . indextype == " AUTHOR " :
if self . indextype == " AUTHOR " :
for key in self . keywords :
for key in self . keywords :
if key in self . authwords :
if key in self . authwords :
@ -94,20 +98,22 @@ class index:
for word in self . authwords . keys ( ) :
for word in self . authwords . keys ( ) :
if word in self . keywords :
if word in self . keywords :
if word == " after " :
if word == " after " :
self . authwords [ word ] = [ re . compile ( r " ^.* %s \ b(.*) " % after ) for after in self . keywords [ word ] ]
self . authwords [ word ] = [ re . compile ( r " ^.* %s \ b(.*) " % after )
for after in self . keywords [ word ] ]
elif word == " sep " :
elif word == " sep " :
self . authwords [ word ] = [ " %s " % sep for sep in self . authwords [ word ] ] + [ " , " ]
self . authwords [ word ] = [ " %s " % sep for sep in self . authwords [ word ] ] + [ " , " ]
self . authwords [ word ] = [ re . compile ( r " ^(.*) %s (.*)$ " % sep ) for sep in self . authwords [ word ] ]
self . authwords [ word ] = [ re . compile ( r " ^(.*) %s (.*)$ " % sep )
for sep in self . authwords [ word ] ]
else :
else :
self . authwords [ word ] = self . keywords [ word ]
self . authwords [ word ] = self . keywords [ word ]
def _raw_add ( self , key , number , link ) :
def _raw_add ( self , key , number , link ) :
( first , key ) = self . filter ( key )
( first , key ) = self . filter ( key )
if not self . data . has_ key( first ) :
if not first in self . data . keys ( ) :
self . data [ first ] = dict ( )
self . data [ first ] = dict ( )
if not self . data [ first ] . has_ key( key ) :
if not key in self . data [ first ] . keys ( ) :
self . data [ first ] [ key ] = [ ]
self . data [ first ] [ key ] = [ ]
self . data [ first ] [ key ] . append ( { ' num ' : number , ' link ' : link } )
self . data [ first ] [ key ] . append ( { ' num ' : number , ' link ' : link } )
def add ( self , key , number , link ) :
def add ( self , key , number , link ) :
if self . indextype == " TITLE " :
if self . indextype == " TITLE " :
@ -116,8 +122,8 @@ class index:
match = pattern . match ( key )
match = pattern . match ( key )
if match :
if match :
self . _raw_add (
self . _raw_add (
" %s ( %s ) " % ( match . group ( 2 ) + match . group ( 3 ) , match . group ( 1 ) ) ,
" %s ( %s ) " % ( match . group ( 2 ) + match . group ( 3 ) ,
number , link )
match . group ( 1 ) ) , number , link )
return
return
self . _raw_add ( key , number , link )
self . _raw_add ( key , number , link )
@ -129,26 +135,26 @@ class index:
self . _raw_add ( author , number , link )
self . _raw_add ( author , number , link )
def refToStr ( self , ref ) :
def refToStr ( self , ref ) :
if sys . version_info > = ( 2 , 6 ) :
if sys . version_info > = ( 2 , 6 ) :
return ' \\ hyperlink {{ {0[link]} }} {{ {0[num]} }} ' . format ( ref )
return ' \\ hyperlink {{ {0[link]} }} {{ {0[num]} }} ' . format ( ref )
else :
else :
return ' \\ hyperlink { %(link)s } { %(num)s } ' % ref
return ' \\ hyperlink { %(link)s } { %(num)s } ' % ref
def entryToStr ( self , key , entry ) :
def entryToStr ( self , key , entry ) :
if sys . version_info > = ( 2 , 6 ) :
if sys . version_info > = ( 2 , 6 ) :
return unicode ( ' \\ idxentry {{ {0} }} {{ {1} }} \n ' ) . format ( key , ' \\ \\ ' . join ( map ( self . refToStr , entry ) ) )
return unicode ( ' \\ idxentry {{ {0} }} {{ {1} }} \n ' ) . format ( key , ' \\ \\ ' . join ( map ( self . refToStr , entry ) ) )
else :
else :
return unicode ( ' \\ idxentry { %s } { %s } \n ' ) % ( key , ' \\ \\ ' . join ( map ( self . refToStr , entry ) ) )
return unicode ( ' \\ idxentry { %s } { %s } \n ' ) % ( key , ' \\ \\ ' . join ( map ( self . refToStr , entry ) ) )
def idxBlockToStr ( self , letter , entries ) :
def idxBlockToStr ( self , letter , entries ) :
str = ' \\ begin {idxblock} { ' + letter + ' } ' + ' \n '
string = ' \\ begin {idxblock} { ' + letter + ' } ' + ' \n '
for key in sorted ( entries . keys ( ) , key = sortkey ) :
for key in sorted ( entries . keys ( ) , key = sortkey ) :
str + = self . entryToStr ( key , entries [ key ] )
string + = self . entryToStr ( key , entries [ key ] )
str + = ' \\ end {idxblock} ' + ' \n '
string + = ' \\ end {idxblock} ' + ' \n '
return str
return string
def entriesToStr ( self ) :
def entriesToStr ( self ) :
str = " "
string = " "
for letter in sorted ( self . data . keys ( ) ) :
for letter in sorted ( self . data . keys ( ) ) :
str + = self . idxBlockToStr ( letter , self . data [ letter ] )
string + = self . idxBlockToStr ( letter , self . data [ letter ] )
return str
return string