Quote:
Originally Posted by BeckyEbook
I need a precise offset for the found error in the validation plugin.
Is it possible?
plugin.py line 59:
Code:
bk.add_result(escape(filename), linenumber, None, 'Becky Error #01: ' + message)
|
When KevinH added validation support, he kindly provided the required code, which I slightly updated.
Code:
iswindows = sys.platform.startswith('win')
# code provided by KevinH
def generate_line_offsets(s):
offlst = [0]
i = s.find('\n', 0)
while i >= 0:
offlst.append(i)
i = s.find('\n', i + 1)
return offlst
# code provided by KevinH
def charoffset(line, col, offlst):
coffset = None
if iswindows:
coffset = offlst[line-1] + 2 + (col - 1) - line
else:
coffset = offlst[line-1] + 1 + (col - 1)
if line == 1:
coffset -= 1
return coffset
Basically, you run
generate_line_offsets(s) once with the text of the HTML file as the input and then use this value together with line and column numbers as the input for
charoffset(line, col, offlst).
To see it in action, have a look at the
epubcheck plugin code and the
Regex tester plugin code, which is somewhat similar to your plugin.