# ============================================================================== # This file contains the following functions: # 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("element.nm") # ============================================================================== # html_add_markup_brackets(): 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: # $1 The markup text (without <>). # ============================================================================== define html_add_markup_brackets { full_directive = $1 start = $cursor directive = full_directive 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 ($selection_start != -1 && $selection_left == -1) { # we have a non-rectangular selection start = $selection_start s = "<" full_directive ">" get_selection() "" replace_selection(s) start = start + length(s) } else if ($selection_left == -1) { # we do not have a selection insert_string("<" full_directive ">") start = start + length(full_directive) + 2 } set_cursor_pos(start) } # ============================================================================== # 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 ">") 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 { full_directive = string_dialog("Markup", "OK", "Cancel") if ($string_dialog_button == 1 && full_directive != "") { html_add_markup_brackets(full_directive) deselect_all() } } # ============================================================================== # html_make_A_NAME(): converts the selected text into an HTML anchor, such that # text like "hi there" becomes "hi there". # ============================================================================== define html_make_A_NAME { s = "" if ($selection_start != -1) { s = get_selection() } markup = "a name=\""s"\"" html_add_markup_brackets(markup) } # ============================================================================== # 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. # ============================================================================== $html_simple_entity_list = \ "&&" "<<" ">>" "\""" $html_entity_list = \ "  " "¡¡" "¢¢" "££" \ "¤¤" "¥¥" "¦¦" "§§" \ "¨¨" "©©" "ªª" "««" \ "¬¬" "­­" "®®" "¯¯" \ "°°" "±±" "²²" "³³" \ "´´" "µµ" "¶¶" "··" \ "¸¸" "¹¹" "ºº" "»»" \ "¼¼" "½½" "¾¾" "¿¿" \ "ÀÀ" "ÁÁ" "ÂÂ" "ÃÃ" \ "ÄÄ" "ÅÅ" "ÆÆ" "ÇÇ" \ "ÈÈ" "ÉÉ" "ÊÊ" "ËË" \ "ÌÌ" "ÍÍ" "ÎÎ" "ÏÏ" \ "ÐÐ" "ÑÑ" "ÒÒ" "ÓÓ" \ "ÔÔ" "ÕÕ" "ÖÖ" "××" \ "ØØ" "ÙÙ" "ÚÚ" "ÛÛ" \ "ÜÜ" "ÝÝ" "ÞÞ" "ßß" \ "àà" "áá" "ââ" "ãã" \ "ää" "åå" "ææ" "çç" \ "èè" "éé" "êê" "ëë" \ "ìì" "íí" "îî" "ïï" \ "ð÷ð" "ññ" "òò" "óó" \ "ôô" "õõ" "öö" "÷÷" \ "øø" "ùù" "úú" "ûû" \ "üü" "ýý" "þþ" "ÿÿ" \ "" $html_entity_list_n_elems = 0 # ============================================================================== # html_entities(): converts invalid characters in the selected region to the # equivalent character entities. # ============================================================================== define html_entities { 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() } if ($html_entity_list_n_elems == 0) { # make sure this is set $html_entity_list_n_elems = element_n($html_entity_list, ";") } len = length(s) n = $html_entity_list_n_elems if (len > n) { if (all) { # go through the $html_simple_entity_list table, trying to substitute the # character for the entity within the string each time entity_list = $html_simple_entity_list for (pos = search_string(entity_list, ";", 0); \ pos != -1; \ pos = search_string(entity_list, ";", 0)) { c = substring(entity_list, 0, 1) entity = substring(entity_list, 1, pos+1) entity_list = substring(entity_list, pos+1) if (search_string(s, c, 0, "case") != -1) s = replace_in_string(s, c, entity, "case") } } # go through the $html_entity_list table, trying to substitute the # character for the entity within the string each time entity_list = $html_entity_list for (pos = search_string(entity_list, ";", 0); \ pos != -1; \ pos = search_string(entity_list, ";", 0)) { c = substring(entity_list, 0, 1) entity = substring(entity_list, 1, pos+1) entity_list = substring(entity_list, pos+1) 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) entity_match = elem_of_elempref($html_entity_list, c, ";") if (entity_match != "") ss = ss substring(entity_match, 1) ";" else if (all) { entity_match = elem_of_elempref($html_simple_entity_list, c, ";") if (entity_match != "") ss = ss substring(entity_match, 1) ";" else ss = ss c } else ss = ss c } s = ss } return s }