[quote=āC0D312ā]Would it help if you used
tags?
(Iām just butting into this topic since I understand ~1% of it.)[/quote]
PRE may be an alternative, but it presents other issues in terms of formatting within them (I think). Iām hoping, having replaced all spaces with Ā , that this issue is resolved. But this is more to do with the way HTML behaves, rather than a faulty approach within my code
I should look again at just leaving the spaces, but they tend to *disappear *quite often, he, he. And creating empty elements of a certain size is a minefield - which probably wonāt work anyway.
I was going to suggest that you canāt go wrong with tabsā¦ but tabs are not always four spaces, Doh! Another issue for laterā¦
@facelessuser: I havenāt updated my repo recently. My recent code is below, but I want to fix the disappearing lines issue before I update. That is, if thereās a completely blank line (when not printing line numbers) I want to insert a Ā to prevent it collapsing.
[code]import sublime, sublime_plugin
from xml.dom import minidom
from os import path
class PrintHtmlCommand(sublime_plugin.TextCommand):
def run(self, edit, numbers): # numbers == True: output line numbers
self.colours = {}
path_packages = sublime.packages_path()
settings = sublime.load_settings(āPreferences.sublime-settingsā)
colour_scheme = settings.get(ācolor_schemeā)
colour_scheme = path.normpath(colour_scheme)
colour_scheme = colour_scheme.replace(āPackagesā, āā)
font_size = settings.get(āfont_sizeā) or 10
font_face = settings.get(āfont_faceā) or āConsolasā
tab_size = settings.get(ātab_sizeā) or 4
padd_top = settings.get(āline_padding_topā) or 0
padd_bottom = settings.get(āline_padding_bottomā) or 0
doc = minidom.parse(path_packages + colour_scheme)
the_dict = doc.getElementsByTagName(ādictā)[0]
the_array = the_dict.getElementsByTagName(āarrayā)[0]
colour_settings = the_array.getElementsByTagName(ādictā)[0]
bground = āā; fground = āā; gfground = āā
for key_tag in colour_settings.getElementsByTagName(ākeyā):
try:
if key_tag.firstChild.data.strip() == ābackgroundā:
bground = key_tag.nextSibling.nextSibling.firstChild.data.strip()
elif key_tag.firstChild.data.strip() == āforegroundā:
fground = key_tag.nextSibling.nextSibling.firstChild.data.strip()
elif key_tag.firstChild.data.strip() == āgutterForegroundā:
gfground = key_tag.nextSibling.nextSibling.firstChild.data.strip()
except:
pass
if gfground == āā: gfground = fground
dict_items = the_array.getElementsByTagName(ādictā)[1:]
for item in dict_items:
scope = āā; colour = āā
for key_tag in item.getElementsByTagName(ākeyā):
try:
if key_tag.firstChild.data.strip() == āscopeā:
scope = key_tag.nextSibling.nextSibling.firstChild.data.strip()
elif key_tag.firstChild.data.strip() == āforegroundā:
colour = key_tag.nextSibling.nextSibling.firstChild.data.strip()
except:
pass
if scope != āā and colour != āā:
self.colours[scope] = colour
curr_view = self.view
curr_file = curr_view.file_name()
if curr_file is None:
fname = 'temp'
else:
head, tail = path.split(curr_file)
fname, ext = path.splitext(tail)
fname = fname + ext.replace('.', '_')
fname = head + path.sep + fname
curr_sel = curr_view.sel()[0]
if curr_sel.empty() or abs(curr_sel.end() - curr_sel.begin()) < 4:
the_html = open(fname + '_parsed.html', 'w')
size = curr_view.size()
pt = 0; end = 1
partial = False
else:
the_html = open(fname + '_part.html', 'w')
size = curr_sel.end()
pt = curr_sel.begin()
end = pt + 1
curr_row = curr_view.rowcol(pt)[0] + 1
partial = True
the_html.write('<!DOCTYPE html>\n')
the_html.write('<html>\n<head>\n<title>' + fname + '</title>\n')
the_html.write('<style type=\"text/css\">\n')
the_html.write('\tspan { display: inline; border: 0; margin: 0; padding: 0; }\n')
if not numbers:
the_html.write('\tol { list-style-type: none; }\n')
the_html.write('\tli { color: ' + gfground + '; margin-top: ' +
`padd_top` + 'pt; margin-bottom: ' + `padd_bottom` + 'pt; }\n')
the_html.write('\tbody { ')
if fground != '': the_html.write('color: ' + fground + ';')
if bground != '': the_html.write(' background-color: ' + bground + ';')
the_html.write(' font: ' + `font_size` + 'pt \"' + font_face + '\", Consolas, Monospace;')
the_html.write('\n}\n')
the_html.write('</style>\n</head>\n<body>\n')
if numbers and partial:
the_html.write('<ol>\n<li value="%d">' % curr_row) # use code's line numbering
else:
the_html.write('<ol>\n<li>')
default_scope = curr_view.scope_name(end).split(' ')[0]
while end <= size:
scope_name = curr_view.scope_name(pt)
while curr_view.scope_name(end) == scope_name and end <= size:
end += 1
region = sublime.Region(pt, end)
the_key = scope_name.strip()
if self.colours.has_key(the_key):
the_colour = self.colours[the_key]
else:
if the_key == default_scope:
self.colours[the_key] = fground
the_colour = fground
else:
best_match = 0
for key in self.colours:
if curr_view.score_selector(pt, key) > best_match:
best_match = curr_view.score_selector(pt, key)
the_colour = self.colours[key]
if curr_view.score_selector(pt, default_scope) > best_match:
the_colour = fground
self.colours[the_key] = the_colour
tidied_text = curr_view.substr(region)
tidied_text = tidied_text.replace('&', '&')
tidied_text = tidied_text.replace('<', '<')
tidied_text = tidied_text.replace('>', '>')
tidied_text = tidied_text.replace('\t', ' ' * tab_size)
tidied_text = tidied_text.replace(' ', ' ')
new_li = '</span></li>\n<li><span style=\"color:' + the_colour + '\">'
if len(tidied_text) == 2: print ',' + tidied_text + ','
tidied_text = tidied_text.replace('\n', new_li)
the_html.write('<span style=\"color:' + the_colour + '\">')
the_html.write(tidied_text + '</span>')
pt = end
end = pt + 1
the_html.write('</li>\n</ol>')
the_html.write('\n</body>\n</html>')
the_html.close()[/code]