forked from neora/textuali-avnery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake-heads.py
More file actions
65 lines (56 loc) · 2.51 KB
/
make-heads.py
File metadata and controls
65 lines (56 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import re, json,logging,os,pystache,textualibooks,sys, shutil
logging.basicConfig(level=logging.DEBUG)
logger=logging.getLogger('make-heads')
stache = pystache.Renderer(search_dirs='.',file_encoding='utf-8',string_encoding='utf-8',file_extension=False)
headp = re.compile("^.*<\s*div.*class=\"pagelive\"\s*>",re.DOTALL)
tailp = re.compile("(?<=.)<\s*\/\s*body\s*>.*$",re.DOTALL)
yespat = re.compile("^y(es)?$",re.IGNORECASE)
def make_heads(textualibook,bookhtmls):
for filename in os.listdir(bookhtmls):
htmfile = bookhtmls+"/"+filename
bkp = textualibook.srcpath+"/htmls_backup"
if not os.path.isdir(bkp):
logger.info("creating back up directory: "+bkp)
os.makedirs(bkp)
if os.path.isfile(htmfile):
logger.info("backing up "+htmfile)
shutil.copyfile(htmfile,bkp+"/"+filename)
with open(htmfile,'r+') as t:
top = re.sub(headp,stache.render(stache.load_template('htmhead.html'),textualibook.htm_template_data(filename)).encode('utf-8').rstrip(),t.read())
w = ""
if re.search(tailp,top) :
w = re.sub(tailp,'</body>\n</html>',top)
else:
w = top+'\n</body>\n</html>'
t.seek(0)
t.write(w)
t.truncate()
t.close()
logger.info(filename+" done")
def fix_book_htms(authid,bookid,conf):
htmpath = os.path.join(conf['front']['srcs_dir'],authid+"/"+bookid+"/html")
if not os.path.exists(htmpath):
logger.info("can't find htmls in "+authid+" "+bookid)
return
book = textualibooks.TextualiBook(bookid,authid,conf)
make_heads(book,htmpath)
if __name__=='__main__':
args = sys.argv[1:]
conf = json.load(file('config.json'))
if len(args) < 1 or len(args) > 2:
print("usage: python make-auth.py <author> [<book>]")
quit()
authid = args[0]
if authid not in conf['authors']:
logger.error("sorry "+authid+" is not listed in config.json")
quit()
if len(args) == 1:
conti = raw_input("run the wrap script on all "+authid+" files Y/N? ")
if re.match(yespat,conti):
for bookid in conf['authors'][authid]['books'].iterkeys():
fix_book_htms(authid,bookid,conf)
else:
bookid = args[1]
conti = raw_input("run the wrap script only on "+authid+" "+bookid+"? ")
if re.match(yespat,conti):
fix_book_htms(authid,bookid,conf)