NEDIT_require_macro_file("DEBUG.nm") $DEBUG_ENABLE = 1 NEDIT_require_macro_file("html.nm") $HStoHTML_HTML_win = "" $HStoHTML_this_win = "" # ============================================================================== # HighlightToHTML(): takes the current window and creates a new one with HTML # markup matching the syntax highlighting. If the word "detab" is supplied # as a parameter, detab the output using $tab_dist as the tab setting. # ============================================================================== define HighlightToHTML { color = "" spaces = "" # for tab expansion: must start empty bold = 0 italic = 0 color_change = 0 old_color = "" old_bold = 0 old_italic = 0 chunksize = 200 cursor = 0 do_detab = 0 if ($n_args > 0 && $1 == "detab") do_detab = 1 if (highlight_pattern_extends_from(0) == 0) { dialog("Highlighting is turned off.\nPlain HTML conversion only.") } this_win = $file_path $file_name $HStoHTML_this_win = this_win set_read_only("on") new() # can't give it a title: new($file_name ".html") focus_window("last") HTML_win = $file_path $file_name $HStoHTML_HTML_win = HTML_win set_language_mode("HTML") replace_range(cursor, cursor, "
\n")
cursor = cursor + length("\n")
set_cursor_pos(cursor)
set_read_only("on")
focus_window(this_win)
pos = 0
new_pos = 0
resH = "" # HTML Markup
resT = "" # Text
old_color = highlight_color_value_of_pos(pos)
markOpen = ""
markClose = ""
while (pos < $text_length)
{
color = highlight_color_value_of_pos(pos)
color_change = (color != old_color)
old_color = color
bold = highlight_style_of_pos_is_bold(pos)
italic = highlight_style_of_pos_is_italic(pos)
new_pos = pos + highlight_pattern_extends_from(pos)
# if highlighting is not set, just convert to html as plainly as possible
if (pos == new_pos)
{
new_pos = pos + chunksize
if (new_pos > $text_length)
new_pos = $text_length
}
# skip spaces here
while (search_string(" \t\r\n", get_character(new_pos), 0) != -1)
new_pos++
# end skip spaces
extract = get_range(pos, new_pos)
# tab expansion ============================================================
DbgPos = DEBUG_mark_to()
#DEBUG("[["DEBUG_s(extract)"]]\n")
DEBUG(DEBUG_s_nl_col(extract),"highlight")
DEBUG("\nstart extract = "pos", end extract = "new_pos"\n")
if (do_detab && search_string(extract, "\t", 0) != -1)
{
# panic: tab attack!
# make sure spaces is set up
if (spaces == "")
{
for (spaces = " "; length(spaces) < $tab_dist; spaces = spaces spaces)
continue
}
# get portion of line in front of the extract
lstart = pos
while (get_character(lstart) != "\n" && lstart >= 0)
lstart--
lstart++
# lstart is first character on the line containing pos
col = 0
new_col = 0
cur = lstart
# analyse start of line to determine start col of extract
if(cur"
resH = resH markClose
markClose = ""
if (color_change)
{
markOpen = markOpen ""
}
if (bold)
{
markOpen = markOpen ""
markClose = "" markClose
}
if (italic)
{
markOpen = markOpen ""
markClose = "" markClose
}
if (new_pos == $text_length)
color_change = 1
# add opening markup, and marker for inserting text
resH = resH markOpen "\f"
resT = resT extract "\f"
markOpen = ""
if (length(resT) >= chunksize)
{
resH = resH markClose "\f"
resT = resT "\f"
markClose = ""
focus_window(HTML_win)
set_read_only("off")
cursor = HighlightToHTML_Merge(cursor, resH, resT)
set_read_only("on")
focus_window(this_win)
resT = ""
resH = ""
}
}
# close any markup from last loop
if (color_change)
markClose = markClose ""
focus_window(HTML_win)
resH = resH markClose "\f"
resT = resT "\f"
set_read_only("off")
cursor = HighlightToHTML_Merge(cursor, resH, resT)
replace_range(cursor, cursor, "\n \n")
cursor = cursor + length("\n\n")
set_cursor_pos(cursor)
focus_window(this_win)
set_read_only("off")
return cursor
}
# ==============================================================================
# HighlightToHTML_Merge(resH, resT): helper routine.
# ==============================================================================
define HighlightToHTML_Merge
{
cursor = $1
resH = $2
resT = html_entities($3, "all")
posH = 0
endH = 0
posT = 0
endT = 0
res = ""
for (endH = search_string(resH, "\f", posH); \
endH != -1; \
endH = search_string(resH, "\f", posH))
{
endT = search_string(resT, "\f", posT)
res = res substring(resH, posH, endH) substring(resT, posT, endT)
posH = endH + 1
posT = endT + 1
}
got_bb = 0
got_ii = 0
for (got_bb = search_string(res, "", 0, "case"), \
got_ii = search_string(res, "", 0, "case"); \
got_bb != -1 || got_ii != -1; \
got_bb = search_string(res, "", 0, "case"), \
got_ii = search_string(res, "", 0, "case"))
{
if (got_bb != -1)
res = replace_in_string(res, "", "", "case")
if (got_ii != -1)
res = replace_in_string(res, "", "", "case")
}
replace_range(cursor, cursor, res)
cursor = cursor + length(res)
set_cursor_pos(cursor)
return cursor
}
# ==============================================================================
# HighlightToHTML_PrettyPage(): calls HighlightToHTML(expTabs, pageBg, rulesBg,
# preBg) to provide the text of a file in a block, within a table
# following a heading holding the file name.
# ==============================================================================
define HighlightToHTML_PrettyPage
{
pageBg = "white"
rulesBg = "white"
preBg = "white"
detab_src = ""
if ($n_args >= 1)
detab_src = $1
if ($n_args >= 2) pageBg = $2
if ($n_args >= 3) rulesBg = $3
if ($n_args >= 4) preBg = $4
pageBg = HighlightToHTML_bgColor(pageBg, "white")
rulesBg = HighlightToHTML_bgColor(rulesBg, "white")
preBg = HighlightToHTML_bgColor(preBg, "white")
fname = html_entities($file_name, "all")
cursor = HighlightToHTML("detab")
focus_window($HStoHTML_HTML_win)
replace_range(cursor, cursor, "\n\n\n\n