mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
cleaning RTF footnote
This commit is contained in:
parent
182d0915b9
commit
3356fe6a8b
@ -16,7 +16,9 @@
|
|||||||
# #
|
# #
|
||||||
#########################################################################
|
#########################################################################
|
||||||
import os, tempfile
|
import os, tempfile
|
||||||
|
|
||||||
from calibre.ebooks.rtf2xml import copy
|
from calibre.ebooks.rtf2xml import copy
|
||||||
|
|
||||||
class Footnote:
|
class Footnote:
|
||||||
"""
|
"""
|
||||||
Two public methods are available. The first separates all of the
|
Two public methods are available. The first separates all of the
|
||||||
@ -35,6 +37,7 @@ class Footnote:
|
|||||||
self.__copy = copy
|
self.__copy = copy
|
||||||
self.__write_to = tempfile.mktemp()
|
self.__write_to = tempfile.mktemp()
|
||||||
self.__found_a_footnote = 0
|
self.__found_a_footnote = 0
|
||||||
|
|
||||||
def __first_line_func(self, line):
|
def __first_line_func(self, line):
|
||||||
"""
|
"""
|
||||||
Print the tag info for footnotes. Check whether footnote is an
|
Print the tag info for footnotes. Check whether footnote is an
|
||||||
@ -47,6 +50,7 @@ class Footnote:
|
|||||||
self.__write_to_foot_obj.write(
|
self.__write_to_foot_obj.write(
|
||||||
'mi<tg<open-att__<footnote<num>%s\n' % self.__footnote_count)
|
'mi<tg<open-att__<footnote<num>%s\n' % self.__footnote_count)
|
||||||
self.__first_line = 0
|
self.__first_line = 0
|
||||||
|
|
||||||
def __in_footnote_func(self, line):
|
def __in_footnote_func(self, line):
|
||||||
"""Handle all tokens that are part of footnote"""
|
"""Handle all tokens that are part of footnote"""
|
||||||
if self.__first_line:
|
if self.__first_line:
|
||||||
@ -68,6 +72,7 @@ class Footnote:
|
|||||||
'mi<mk<footnt-clo\n')
|
'mi<mk<footnt-clo\n')
|
||||||
else:
|
else:
|
||||||
self.__write_to_foot_obj.write(line)
|
self.__write_to_foot_obj.write(line)
|
||||||
|
|
||||||
def __found_footnote(self, line):
|
def __found_footnote(self, line):
|
||||||
""" Found a footnote"""
|
""" Found a footnote"""
|
||||||
self.__found_a_footnote = 1
|
self.__found_a_footnote = 1
|
||||||
@ -81,6 +86,7 @@ class Footnote:
|
|||||||
'mi<mk<footnt-ind<%04d\n' % self.__footnote_count)
|
'mi<mk<footnt-ind<%04d\n' % self.__footnote_count)
|
||||||
self.__write_to_foot_obj.write(
|
self.__write_to_foot_obj.write(
|
||||||
'mi<mk<footnt-ope<%04d\n' % self.__footnote_count)
|
'mi<mk<footnt-ope<%04d\n' % self.__footnote_count)
|
||||||
|
|
||||||
def __default_sep(self, line):
|
def __default_sep(self, line):
|
||||||
"""Handle all tokens that are not footnote tokens"""
|
"""Handle all tokens that are not footnote tokens"""
|
||||||
if self.__token_info == 'cw<nt<footnote__':
|
if self.__token_info == 'cw<nt<footnote__':
|
||||||
@ -91,6 +97,7 @@ class Footnote:
|
|||||||
self.__write_obj.write(
|
self.__write_obj.write(
|
||||||
'tx<nu<__________<%s\n' % num
|
'tx<nu<__________<%s\n' % num
|
||||||
)
|
)
|
||||||
|
|
||||||
def __initiate_sep_values(self):
|
def __initiate_sep_values(self):
|
||||||
"""
|
"""
|
||||||
initiate counters for separate_footnotes method.
|
initiate counters for separate_footnotes method.
|
||||||
@ -102,6 +109,7 @@ class Footnote:
|
|||||||
self.__in_footnote = 0
|
self.__in_footnote = 0
|
||||||
self.__first_line = 0 #have not processed the first line of footnote
|
self.__first_line = 0 #have not processed the first line of footnote
|
||||||
self.__footnote_count = 0
|
self.__footnote_count = 0
|
||||||
|
|
||||||
def separate_footnotes(self):
|
def separate_footnotes(self):
|
||||||
"""
|
"""
|
||||||
Separate all the footnotes in an RTF file and put them at the bottom,
|
Separate all the footnotes in an RTF file and put them at the bottom,
|
||||||
@ -111,58 +119,55 @@ class Footnote:
|
|||||||
bottom of the main file.
|
bottom of the main file.
|
||||||
"""
|
"""
|
||||||
self.__initiate_sep_values()
|
self.__initiate_sep_values()
|
||||||
read_obj = open(self.__file)
|
|
||||||
self.__write_obj = open(self.__write_to, 'w')
|
self.__write_obj = open(self.__write_to, 'w')
|
||||||
self.__footnote_holder = tempfile.mktemp()
|
with open(self.__file) as read_obj:
|
||||||
self.__write_to_foot_obj = open(self.__footnote_holder, 'w')
|
self.__footnote_holder = tempfile.mktemp()
|
||||||
line_to_read = 1
|
self.__write_to_foot_obj = open(self.__footnote_holder, 'w')
|
||||||
while line_to_read:
|
line_to_read = 1
|
||||||
line_to_read = read_obj.readline()
|
while line_to_read:
|
||||||
line = line_to_read
|
line_to_read = read_obj.readline()
|
||||||
self.__token_info = line[:16]
|
line = line_to_read
|
||||||
# keep track of opening and closing brackets
|
self.__token_info = line[:16]
|
||||||
if self.__token_info == 'ob<nu<open-brack':
|
# keep track of opening and closing brackets
|
||||||
self.__ob_count = line[-5:-1]
|
if self.__token_info == 'ob<nu<open-brack':
|
||||||
if self.__token_info == 'cb<nu<clos-brack':
|
self.__ob_count = line[-5:-1]
|
||||||
self.__cb_count = line[-5:-1]
|
if self.__token_info == 'cb<nu<clos-brack':
|
||||||
# In the middle of footnote text
|
self.__cb_count = line[-5:-1]
|
||||||
if self.__in_footnote:
|
# In the middle of footnote text
|
||||||
self.__in_footnote_func(line)
|
if self.__in_footnote:
|
||||||
# not in the middle of footnote text
|
self.__in_footnote_func(line)
|
||||||
else:
|
# not in the middle of footnote text
|
||||||
self.__default_sep(line)
|
else:
|
||||||
|
self.__default_sep(line)
|
||||||
self.__write_obj.close()
|
self.__write_obj.close()
|
||||||
read_obj.close()
|
|
||||||
self.__write_to_foot_obj.close()
|
self.__write_to_foot_obj.close()
|
||||||
read_obj = open(self.__footnote_holder, 'r')
|
with open(self.__footnote_holder, 'r') as read_obj,
|
||||||
write_obj = open(self.__write_to, 'a')
|
open(self.__write_to, 'a') as write_obj:
|
||||||
write_obj.write(
|
write_obj.write(
|
||||||
'mi<mk<sect-close\n'
|
'mi<mk<sect-close\n'
|
||||||
'mi<mk<body-close\n'
|
'mi<mk<body-close\n'
|
||||||
'mi<tg<close_____<section\n'
|
'mi<tg<close_____<section\n'
|
||||||
'mi<tg<close_____<body\n'
|
'mi<tg<close_____<body\n'
|
||||||
'mi<tg<close_____<doc\n'
|
'mi<tg<close_____<doc\n'
|
||||||
'mi<mk<footnt-beg\n')
|
'mi<mk<footnt-beg\n')
|
||||||
line = 1
|
for line in read_obj:
|
||||||
while line:
|
write_obj.write(line)
|
||||||
line = read_obj.readline()
|
write_obj.write(
|
||||||
write_obj.write(line)
|
'mi<mk<footnt-end\n')
|
||||||
write_obj.write(
|
|
||||||
'mi<mk<footnt-end\n')
|
|
||||||
read_obj.close()
|
|
||||||
write_obj.close()
|
|
||||||
os.remove(self.__footnote_holder)
|
os.remove(self.__footnote_holder)
|
||||||
copy_obj = copy.Copy(bug_handler = self.__bug_handler)
|
copy_obj = copy.Copy(bug_handler = self.__bug_handler)
|
||||||
if self.__copy:
|
if self.__copy:
|
||||||
copy_obj.copy_file(self.__write_to, "footnote_separate.data")
|
copy_obj.copy_file(self.__write_to, "footnote_separate.data")
|
||||||
copy_obj.rename(self.__write_to, self.__file)
|
copy_obj.rename(self.__write_to, self.__file)
|
||||||
os.remove(self.__write_to)
|
os.remove(self.__write_to)
|
||||||
|
|
||||||
def update_info(self, file, copy):
|
def update_info(self, file, copy):
|
||||||
"""
|
"""
|
||||||
Unused method
|
Unused method
|
||||||
"""
|
"""
|
||||||
self.__file = file
|
self.__file = file
|
||||||
self.__copy = copy
|
self.__copy = copy
|
||||||
|
|
||||||
def __get_foot_body_func(self, line):
|
def __get_foot_body_func(self, line):
|
||||||
"""
|
"""
|
||||||
Process lines in main body and look for beginning of footnotes.
|
Process lines in main body and look for beginning of footnotes.
|
||||||
@ -172,6 +177,7 @@ class Footnote:
|
|||||||
self.__state = 'foot'
|
self.__state = 'foot'
|
||||||
else:
|
else:
|
||||||
self.__write_obj.write(line)
|
self.__write_obj.write(line)
|
||||||
|
|
||||||
def __get_foot_foot_func(self, line):
|
def __get_foot_foot_func(self, line):
|
||||||
"""
|
"""
|
||||||
Copy footnotes from bottom of file to a separate, temporary file.
|
Copy footnotes from bottom of file to a separate, temporary file.
|
||||||
@ -180,6 +186,7 @@ class Footnote:
|
|||||||
self.__state = 'body'
|
self.__state = 'body'
|
||||||
else:
|
else:
|
||||||
self.__write_to_foot_obj.write(line)
|
self.__write_to_foot_obj.write(line)
|
||||||
|
|
||||||
def __get_footnotes(self):
|
def __get_footnotes(self):
|
||||||
"""
|
"""
|
||||||
Private method to remove footnotes from main file. Read one line from
|
Private method to remove footnotes from main file. Read one line from
|
||||||
@ -203,6 +210,7 @@ class Footnote:
|
|||||||
read_obj.close()
|
read_obj.close()
|
||||||
self.__write_obj.close()
|
self.__write_obj.close()
|
||||||
self.__write_to_foot_obj.close()
|
self.__write_to_foot_obj.close()
|
||||||
|
|
||||||
def __get_foot_from_temp(self, num):
|
def __get_foot_from_temp(self, num):
|
||||||
"""
|
"""
|
||||||
Private method for joining footnotes to body. This method reads from
|
Private method for joining footnotes to body. This method reads from
|
||||||
@ -223,6 +231,7 @@ class Footnote:
|
|||||||
else:
|
else:
|
||||||
if line == look_for:
|
if line == look_for:
|
||||||
found_foot = 1
|
found_foot = 1
|
||||||
|
|
||||||
def __join_from_temp(self):
|
def __join_from_temp(self):
|
||||||
"""
|
"""
|
||||||
Private method for rejoining footnotes to body. Read from the
|
Private method for rejoining footnotes to body. Read from the
|
||||||
@ -242,6 +251,7 @@ class Footnote:
|
|||||||
line = self.__get_foot_from_temp(line[17:-1])
|
line = self.__get_foot_from_temp(line[17:-1])
|
||||||
self.__write_obj.write(line)
|
self.__write_obj.write(line)
|
||||||
read_obj.close()
|
read_obj.close()
|
||||||
|
|
||||||
def join_footnotes(self):
|
def join_footnotes(self):
|
||||||
"""
|
"""
|
||||||
Join the footnotes from the bottom of the file and put them in their
|
Join the footnotes from the bottom of the file and put them in their
|
||||||
|
Loading…
x
Reference in New Issue
Block a user