# ==============================================================================
# This file contains the following functions:
# html__get_doctype
# html_add_markup_brackets
# html_convert_to_markup
# html_ask_for_markup
# html_make_A_NAME
# html_make_A_HREF
# html_entities
# ==============================================================================
NEDIT_require_macro_file("extensions.nm")
NEDIT_require_macro_file("array_utils.nm")
# ==============================================================================
# html__get_doctype(): looks in the current document for a directive,
# stores the result in $html["doctype"][docname], and returns it. If this
# has been done for the document, returns the previous result.
# ==============================================================================
define html__get_doctype
{
$html[""] = ""
if ("doctype" in $html)
doctype = $html["doctype"]
else
doctype = $empty_array
full_name = $file_path $file_name
if (full_name in doctype)
return doctype[full_name]
res = ""
pos = search("(?n\\.*\\>", 0, "regex")
if (pos > 0)
res = get_range(pos, $search_end)
doctype[full_name] = res
$html["doctype"] = doctype
return res
}
# ==============================================================================
# html_add_markup_brackets(full_directive, isblock, beg, end): uses the string
# parameter to either surround the current selection with markup (if there
# is a selection) or to insert the markup as an open/close markup bracket
# pair. The first word of the markup string is used to generate the markup
# terminator.
# Parameters:
# full_directive The markup text (without <>).
# isblock True (non-zero) if newlines should be added.
# beg Position of selection start or cursor
# end Position of selection end
# ==============================================================================
define html_add_markup_brackets
{
full_directive = $1
pos = search_string(full_directive, $sub_sep, 0)
isblock = 0
if (pos >= 0)
{
isblock = substring(full_directive, pos + 1)
full_directive = substring(full_directive, 0, pos)
}
beg = $selection_start
end = $selection_end
if (beg >= 0 && $selection_left >= 0)
return ""
if (beg < 0)
{
beg = $cursor
end = beg
}
if ($n_args > 1)
isblock = $2
if ($n_args > 2)
{
beg = $3
end = beg
}
if ($n_args > 3)
end = $4
directive = replace_in_string(full_directive, "^\\s*(\\w\\S*).*", "\\1", \
"regex", "copy")
# directive_end = -1
# directive_start = search_string(full_directive, "[a-zA-Z0-1]", 0, "regex")
#
# if (directive_start >= 0)
# directive_end = search_string(full_directive, ">", directive_start, "regex")
#
# if (directive_start >= 0 && directive_end >= 0)
# directive = substring(full_directive, directive_start, directive_end)
if (beg != end)
{
# we have a non-rectangular selection
s = "<" full_directive ">" get_range(beg, end) "" directive ">"
replace_range(beg, end, s)
end = beg + length(s)
}
else
{
# we do not have a selection
if (!isblock)
{
replace_range(beg, end, "<" full_directive ">" directive ">")
end = beg + length(full_directive) + 2
}
else
{
set_cursor_pos(beg)
insert_string("<" full_directive ">")
newline_and_indent()
end = $cursor
newline_and_indent()
insert_string("" directive ">")
}
}
set_cursor_pos(end)
return full_directive $sub_sep isblock
}
# ==============================================================================
# html_convert_to_markup(): converts the selection "word1 word2 ..." to
# "", leaving the cursor between the two markup
# brackets.
# ==============================================================================
define html_convert_to_markup
{
full_directive = get_selection()
directive = full_directive
start = $selection_start
directive_end = -1
directive_start = search_string(full_directive, "[a-zA-Z0-1]", 0, "regex")
if (directive_start >= 0)
directive_end = search_string(full_directive, ">", directive_start, "regex")
if (directive_start >= 0 && directive_end >= 0)
directive = substring(full_directive, directive_start, directive_end)
replace_selection("<" full_directive ">" directive ">")
deselect_all()
set_cursor_pos(start + length(full_directive) + 2)
}
# ==============================================================================
# html_ask_for_markup(): requests a markup directive in a dialog box: the user
# should enter the leading directive such as "word1 word2 ...". This is
# converted to "" and placed before the selected text.
# The terminator, "", is added at the end of the selection,
# leaving the cursor at the end of the terminator.
# ==============================================================================
define html_ask_for_markup
{
if ($selection_left >= 0 || $locked)
{
beep()
return # disallow rectangular selections, or application to a locked file
}
if ($selection_start < 0)
{
beg = $cursor
end = $cursor
}
else
{
beg = $selection_start
end = $selection_end
}
set_locked(1)
full_directive = string_dialog("Markup", "Block", "Inline", "Cancel")
res = $string_dialog_button
set_locked(0)
if (full_directive == "" || res < 1 || res > 2)
return ""
res = html_add_markup_brackets(full_directive, res == 1, beg, end)
deselect_all()
return res
}
# ==============================================================================
# html_make_A_NAME(): converts the selected text into an HTML anchor, such that
# text like "hi there" becomes "hi there".
# If there is a suspicion this is an xhtml file, use "id" instead of
# "name".
# ==============================================================================
define html_make_A_NAME
{
s = ""
if ($selection_start != -1)
s = get_selection()
while (!match_re(s, "^[A-Za-z][-A-Za-z0-9:_.]*$"))
{
u = replace_in_string(s, "(?n\\s)", "_", "regex", "copy")
u = replace_in_string(u, "(?n[^-A-Za-z0-9:_.])", ":", "regex", "copy")
u = replace_in_string(u, "^([^A-Za-z])", "name:-\\1", "regex", "copy")
t = string_dialog("String\n "s"\nDoes not conform to norms.\n" \
"It should match [A-Za-z][-A-Za-z0-9:_.]*\n" \
"Do you want to change it?\n" \
"(Suggestion:\n "u"\nfor example)", \
"Change", "Keep", "Use suggested", "Cancel")
r = $string_dialog_button
if (r == 1 && t != "") s = t # Change
else if (r == 2) break # Keep
else if (r == 3) s = u # Use suggested
else
return
}
if (match_si(html__get_doctype(), "xhtml"))
name_or_id = "id=\""s"\" name=\""s"\"" # XML uses ids, HTML uses names
else
name_or_id = "name=\""s"\""
html_add_markup_brackets("a "name_or_id)
}
# ==============================================================================
# html_make_A_HREF(): converts the selected text into an HTML local reference,
# such that text like "hello" becomes "hello".
# ==============================================================================
define html_make_A_HREF
{
s = ""
if ($selection_start != -1)
{
s = get_selection()
}
markup = "a href=\"#"s"\""
html_add_markup_brackets(markup)
}
# ==============================================================================
# Information for the html_entities() function.
# ==============================================================================
# don't include && here: treat it separately since it must be done first
$html_simple_entity_list = \
"<<" ">>" "\"""
$html_entity_list = \
" " "¡¡" "¢¢" "££" \
"¤¤" "¥¥" "¦¦" "§§" \
"¨¨" "©©" "ªª" "««" \
"¬¬" "" "®®" "¯¯" \
"°°" "±±" "²²" "³³" \
"´´" "µµ" "¶¶" "··" \
"¸¸" "¹¹" "ºº" "»»" \
"¼¼" "½½" "¾¾" "¿¿" \
"ÀÀ" "ÁÁ" "ÂÂ" "ÃÃ" \
"ÄÄ" "ÅÅ" "ÆÆ" "ÇÇ" \
"ÈÈ" "ÉÉ" "ÊÊ" "ËË" \
"ÌÌ" "ÍÍ" "ÎÎ" "ÏÏ" \
"ÐÐ" "ÑÑ" "ÒÒ" "ÓÓ" \
"ÔÔ" "ÕÕ" "ÖÖ" "××" \
"ØØ" "ÙÙ" "ÚÚ" "ÛÛ" \
"ÜÜ" "ÝÝ" "ÞÞ" "ßß" \
"àà" "áá" "ââ" "ãã" \
"ää" "åå" "ææ" "çç" \
"èè" "éé" "êê" "ëë" \
"ìì" "íí" "îî" "ïï" \
"ð÷ð" "ññ" "òò" "óó" \
"ôô" "õõ" "öö" "÷÷" \
"øø" "ùù" "úú" "ûû" \
"üü" "ýý" "þþ" "ÿÿ" \
""
$html_entities = $empty_array
# ==============================================================================
# html_entities([string, [, "all"]]): converts invalid characters in the string
# passed (or in the selected region if no arguments given, or the string
# is empty) to the equivalent character entities. If the second argument
# "all" is given, all known entities are substituted; otherwise those
# which may be HTML markup will be skipped ('&', '<', '>', '"').
# ==============================================================================
define html_entities
{
$html_entities[""] = ""
if (!("simple" in $html_entities))
{
entity_list = $empty_array
split_list = split($html_simple_entity_list, "(?<=;)", "regex")
for (ind in split_list)
{
ent = split_list[ind]
if (ent != "")
entity_list[substring(ent, 0, 1)] = substring(ent, 1)
}
$html_entities["simple"] = entity_list
}
if (!("other" in $html_entities))
{
entity_list = $empty_array
split_list = split($html_entity_list, "(?<=;)", "regex")
for (ind in split_list)
{
ent = split_list[ind]
if (ent != "")
entity_list[substring(ent, 0, 1)] = substring(ent, 1)
}
$html_entities["other"] = entity_list
}
s = ""
all = 0
if ($n_args > 0)
s = $1
if ($n_args > 1 && $2 == "all")
all = 1
if (s == "" && $selection_start != -1)
{
s = get_selection()
}
len = length(s)
if (len > $html_entities["other"][])
{
if (all)
{
# start with & -> &
if (search_string(s, "&", 0, "case") != -1)
s = replace_in_string(s, "&", "&", "case")
# go through the $html_entities["simple"] table, trying to substitute the
# character for the entity within the string each time
entity_list = $html_entities["simple"]
for (c in entity_list)
{
entity = entity_list[c]
if (search_string(s, c, 0, "case") != -1)
s = replace_in_string(s, c, entity, "case")
}
}
# go through the $html_entities["other"] table, trying to substitute the
# character for the entity within the string each time
entity_list = $html_entities["other"]
for (c in entity_list)
{
entity = entity_list[c]
if (search_string(s, c, 0, "case") != -1)
s = replace_in_string(s, c, entity, "case")
}
}
else
{
# go through the string to be altered, checking each character to
# see if it should be substituted
ss = ""
for (j = 0; j < len; j++)
{
c = substring(s, j, j+1)
if (c in $html_entities["other"])
ss = ss $html_entities["other"][c]
else if (all)
{
if (c in $html_entities["simple"])
ss = ss $html_entities["simple"][c]
else
ss = ss c
}
else
ss = ss c
}
s = ss
}
return s
}