-- code_pretty.lua -- © 2007 Multi-Fusion Marketing, Inc. and AMS Resource Center, LLC -- Luke Towery (CyberRBT@hotmail.com) -- last Update: 2007-04-01; 2007-04-02; 2007-04-03; 2007-04-15 -- See Project/Actions/OnStartup for REQUIRED variable initializations function StringToTable(DelimitedString, Delimiter) -- minor mod from ir code local tbReturn = {}; local nDelimiterLength = String.Length(Delimiter); local nLength; local nPos = nil; local nTableIndex = 1; local strData; local nLength; local strWorking; if (nDelimiterLength < 1) then tbReturn[nTableIndex] = DelimitedString; return tbReturn; end strWorking = DelimitedString; nPos = String.Find(strWorking, Delimiter); while (nPos ~= -1) do strData = String.Left(strWorking, nPos-1); tbReturn[nTableIndex] = strData; nTableIndex = nTableIndex + 1; nLength = String.Length(strWorking); strWorking = String.Right(strWorking, nLength - (nPos + (nDelimiterLength - 1))); nPos = String.Find(strWorking, Delimiter); end if (strWorking ~= "" ) then tbReturn[nTableIndex] = strWorking; end return tbReturn; end function pretty_comment(in_str) if (in_str == nil) then return ""; end local out_str; if (convert_to_html == true) then out_str = ""; out_str = out_str .. in_str .. ""; if (comment_italic == true) then out_str = "" .. out_str .. ""; end if (comment_bold == true) then out_str = "" .. out_str .. ""; end else out_str = "[COLOR='" .. comment_color .. "']"; out_str = out_str .. in_str .. "[/COLOR]"; if (comment_italic == true) then out_str = "[i]" .. out_str .. "[/i]"; end if (comment_bold == true) then out_str = "[b]" .. out_str .. "[/b]"; end end return out_str; end function pretty_quoted(in_str) if (in_str == nil) then return ""; end local out_str; if (convert_to_html == true) then out_str = ""; out_str = out_str .. in_str .. ""; if (quoted_italic == true) then out_str = "" .. out_str .. ""; end if (quoted_bold == true) then out_str = "" .. out_str .. ""; end else out_str = "[COLOR='" .. quoted_color .. "']"; out_str = out_str .. in_str .. "[/COLOR]"; if (quoted_italic == true) then out_str = "[i]" .. out_str .. "[/i]"; end if (quoted_bold == true) then out_str = "[b]" .. out_str .. "[/b]"; end end return out_str; end function pretty_keyword(in_str) if (in_str == nil) then return ""; end local out_str; if (convert_to_html == true) then out_str = ""; out_str = out_str .. in_str .. ""; if (keyword_italic == true) then out_str = "" .. out_str .. ""; end if (keyword_bold == true) then out_str = "" .. out_str .. ""; end else out_str = "[COLOR='" .. keyword_color .. "']"; out_str = out_str .. in_str .. "[/COLOR]"; if (keyword_italic == true) then out_str = "[i]" .. out_str .. "[/i]"; end if (keyword_bold == true) then out_str = "[b]" .. out_str .. "[/b]"; end end return out_str; end function pretty_punctuation(in_str) if (in_str == nil) then return ""; end local out_str; if (convert_to_html == true) then out_str = ""; out_str = out_str .. in_str .. ""; if (punctuation_italic == true) then out_str = "" .. out_str .. ""; end if (punctuation_bold == true) then out_str = "" .. out_str .. ""; end else out_str = "[COLOR='" .. punctuation_color .. "']"; out_str = out_str .. in_str .. "[/COLOR]"; if (punctuation_italic == true) then out_str = "[i]" .. out_str .. "[/i]"; end if (punctuation_bold == true) then out_str = "[b]" .. out_str .. "[/b]"; end end return out_str; end function prep_punctuation_all(in_str) -- the easy punctuation if (in_str == nil) then return ""; end local this_str = in_str; this_str = String.Replace(this_str, "/", "@div@"); this_str = String.Replace(this_str, ",", "@comma@"); this_str = String.Replace(this_str, "<", "@less@"); this_str = String.Replace(this_str, ">", "@greater@"); this_str = String.Replace(this_str, "=", "@equals@"); this_str = String.Replace(this_str, "[", "@brace_left@"); this_str = String.Replace(this_str, "]", "@brace_right@"); this_str = String.Replace(this_str, "{", "@bracket_left@"); this_str = String.Replace(this_str, "}", "@bracket_right@"); this_str = String.Replace(this_str, "(", "@paren_left@"); this_str = String.Replace(this_str, ")", "@paren_right@"); this_str = String.Replace(this_str, "~", "@tilde@"); this_str = String.Replace(this_str, "-", "@minus@"); this_str = String.Replace(this_str, "+", "@plus@"); this_str = String.Replace(this_str, "*", "@star@"); this_str = String.Replace(this_str, ".", "@dot@"); this_str = String.Replace(this_str, "'", "@squote@"); return this_str; end function prefix_lineNbr(nbr, maxlines) if (maxlines < 100) then if (nbr < 10) then nbr = "0" .. nbr; end else if (nbr < 10) then nbr = "00" .. nbr; elseif (nbr < 100) then nbr = "0" .. nbr; end end nbr = nbr .. pad_number_lines; return nbr; end function isCharAlpha(char_test) if ((String.Asc(char_test) >= 65) and (String.Asc(char_test) <= 90)) then return true; end if ((String.Asc(char_test) >= 97) and (String.Asc(char_test) <= 122)) then return true; end return false; end function isKeyword(str_test) local result = false; for i = 1, Table.Count(keywords) do if (String.CompareNoCase(str_test, keywords[i]) == 0) then result = true; break; end end return result; end function pretty_one_line(str_in) -- the big one local part_comment = ""; local pretty_done = ""; local isEven = false; local word_parts = {}; local quoted_parts = {}; local LeftTrim = ""; local LeftTrimLength = 0; local RightTrim = ""; local RightTrimLength = 0; local i; local j; -- prep for nil issues str_in = String.Replace(str_in, "nil,", "@nilcomma@"); str_in = String.Replace(str_in, "nil)", "@nilparen@"); str_in = String.Replace(str_in, "nil;", "@nilsemi@"); str_in = String.Replace(str_in, "nil ", "@nilspace@"); str_in = String.Replace(str_in, "\"\\\\\"", "@doubleslash_quote@"); str_in = String.Replace(str_in, "\\\"", "@quote@"); --\\" -- extract and prettify an inline comment local nPos = String.Find(str_in, str_comment_token); if (nPos > 0) then local offset = -1; if (nPos > 1) then offset = -2; end part_comment = String.Right(str_in, String.Length(str_in) - nPos + String.Length(str_comment_token)); part_comment = pretty_comment(part_comment); str_in = String.Left(str_in, nPos + offset); end -- process remainder of the line local quoted_parts = StringToTable(str_in, "\""); -- \\" for i = 1, Table.Count(quoted_parts) do -- prettify quoted strings if (isEven == true) then quoted_parts[i] = "\"" .. quoted_parts[i] .. "\""; quoted_parts[i] = pretty_quoted(quoted_parts[i]); else -- prettify (easy) punctuation quoted_parts[i] = prep_punctuation_all(quoted_parts[i]); -- prettify keywords word_parts = StringToTable(quoted_parts[i], " "); for j = 1, Table.Count(word_parts) do -- prep for left white space if (String.TrimLeft(word_parts[j]) ~= word_parts[j]) then LeftTrimLength = String.Length(word_parts[j]) - String.Length(String.TrimLeft(word_parts[j])); LeftTrim = String.Left(word_parts[j], LeftTrimLength); word_parts[j] = String.TrimLeft(word_parts[j]); else LeftTrim = ""; end -- prep for right white space if (String.TrimRight(word_parts[j]) ~= word_parts[j]) then RightTrimLength = String.Length(word_parts[j]) - String.Length(String.TrimRight(word_parts[j])); RightTrim = String.Right(word_parts[j], RightTrimLength); word_parts[j] = String.TrimRight(word_parts[j]); else RightTrim = ""; end -- keywords now... if (isKeyword(word_parts[j]) == true) then word_parts[j] = pretty_keyword(word_parts[j]); end -- restore whitespace word_parts[j] = LeftTrim .. word_parts[j] .. RightTrim; end -- reconstruct this quoted parts element quoted_parts[i] = Table.Concat(word_parts, " ", 1, TABLE_ALL) .. " "; end isEven = not isEven; end -- now, put then entire line back together pretty_done = Table.Concat(quoted_parts, "", 1, TABLE_ALL) .. part_comment; -- and, recover from punctuation issues pretty_done = String.Replace(pretty_done, "@doubleslash_quote@", pretty_quoted("\"\\\\\"")); pretty_done = String.Replace(pretty_done, "@quote@", "\\\""); --\" pretty_done = String.Replace(pretty_done, "@div@", pretty_punctuation("/")); pretty_done = String.Replace(pretty_done, "@comma@", pretty_punctuation(",")); pretty_done = String.Replace(pretty_done, "@less@", pretty_punctuation("<")); pretty_done = String.Replace(pretty_done, "@greater@", pretty_punctuation(">")); pretty_done = String.Replace(pretty_done, "@equals@", pretty_punctuation("=")); pretty_done = String.Replace(pretty_done, "@brace_left@", pretty_punctuation("[")); pretty_done = String.Replace(pretty_done, "@brace_right@", pretty_punctuation("]")); pretty_done = String.Replace(pretty_done, "@bracket_left@", pretty_punctuation("{")); pretty_done = String.Replace(pretty_done, "@bracket_right@", pretty_punctuation("}")); pretty_done = String.Replace(pretty_done, "@paren_left@", pretty_punctuation("(")); pretty_done = String.Replace(pretty_done, "@paren_right@", pretty_punctuation(")")); pretty_done = String.Replace(pretty_done, "@tilde@", pretty_punctuation("~")); pretty_done = String.Replace(pretty_done, "@minus@", pretty_punctuation("-")); pretty_done = String.Replace(pretty_done, "@plus@", pretty_punctuation("+")); pretty_done = String.Replace(pretty_done, "@star@", pretty_punctuation("*")); pretty_done = String.Replace(pretty_done, "@dot@", pretty_punctuation(".")); pretty_done = String.Replace(pretty_done, "@squote@", pretty_punctuation("'")); -- bug fix pretty_done = String.Replace(pretty_done, "( \"", "(\""); -- now, the nil issues pretty_done = String.Replace(pretty_done, "@nilcomma@", pretty_keyword("nil") .. pretty_punctuation(",")); pretty_done = String.Replace(pretty_done, "@nilparen@", pretty_keyword("nil") .. pretty_punctuation(")")); pretty_done = String.Replace(pretty_done, "@nilsemi@", pretty_keyword("nil") .. ";"); pretty_done = String.Replace(pretty_done, "@nilspace@", pretty_keyword("nil") .. " "); -- finally, all done! return pretty_done; end function xlate_FShtmlToBB() if ((html_fontsize == "6") or (html_fontsize == "7")) then return "1"; elseif (html_fontsize == "8") then return "2"; elseif (html_fontsize == "9") then return "3"; elseif (html_fontsize == "10") then return "4"; elseif (html_fontsize == "11") then return "5"; elseif (html_fontsize == "12") then return "6"; else return "7"; end end function sort_no_case(v1, v2) if (String.Lower(v1) < String.Lower(v2))then return true; else return false; end end function pretty_lines_function_list(this_lua) local pretty_all_done = ""; local f_list = {}; local f_test = ""; local nPos = 0; for line = 1, Table.Count(this_lua) do f_test = String.Lower(String.TrimLeft(this_lua[line])); nPos = String.Find(f_test, "function"); if (nPos == 1) then f_test = String.TrimLeft(this_lua[line]); Table.Insert(f_list, Table.Count(f_list) + 1, pretty_one_line(f_test)); end end if (conv_category == "Function List Sorted") then Table.Sort(f_list, sort_no_case); end for line = 1, Table.Count(f_list) do if (number_lines == true) then f_list[line] = prefix_lineNbr(line, Table.Count(f_list)) .. f_list[line]; if (convert_to_html == false) then f_list[line] = String.Replace(f_list[line], " ", ""); end end if (convert_to_html == true) then pretty_all_done = pretty_all_done .. "@tab_double@" .. f_list[line] .. "
\r\n"; pretty_all_done = String.Replace(pretty_all_done, "\t", pad_tab); pretty_all_done = String.Replace(pretty_all_done, " ", "  "); else pretty_all_done = pretty_all_done .. f_list[line] .. "\r\n"; end end if (convert_to_html == true) then pretty_all_done = String.Replace(pretty_all_done, "@tab_double@", html_indent); pretty_all_done = html_indent .. font_style .. pretty_all_done .. html_indent .."\r\n"; else pretty_all_done = "[CODE]\r\n" .. pretty_all_done .. "[/CODE]\r\n"; pretty_all_done = font_style_bb .. pretty_all_done .. "[/SIZE][/FONT]"; pretty_all_done = pretty_all_done .. "[SIZE='1']output enhanced with [URL=http://www.indigorose.com/forums/showthread.php?t=19409]AMS Code Pretty[/URL][/SIZE]\n\n\n"; end return pretty_all_done; end function pretty_lines_ini_file(this_ini) local pretty_all_done = ""; local s_test = ""; local nPos = 0; for line = 1, Table.Count(this_ini) do s_test = String.TrimLeft(this_ini[line]); nPos = String.Find(s_test, "["); if (nPos == 1) then s_test = pretty_keyword(this_ini[line]); else nPos = String.Find(s_test, ";"); if (nPos == 1) then s_test = pretty_comment(this_ini[line]); else s_test = String.Replace(s_test, "=", pretty_punctuation("=")); end end this_ini[line] = s_test; if (number_lines == true) then this_ini[line] = prefix_lineNbr(line, Table.Count(this_ini)) .. this_ini[line]; if (convert_to_html == false) then this_ini[line] = String.Replace(this_ini[line], " ", ""); end end if (convert_to_html == true) then pretty_all_done = pretty_all_done .. "@tab_double@" .. this_ini[line] .. "
\r\n"; pretty_all_done = String.Replace(pretty_all_done, "\t", pad_tab); pretty_all_done = String.Replace(pretty_all_done, " ", "  "); else pretty_all_done = pretty_all_done .. this_ini[line] .. "\r\n"; end end if (convert_to_html == true) then pretty_all_done = String.Replace(pretty_all_done, "@tab_double@", html_indent); pretty_all_done = html_indent .. font_style .. pretty_all_done .. html_indent .."\r\n"; else pretty_all_done = "[CODE]\r\n" .. pretty_all_done .. "[/CODE]\r\n"; pretty_all_done = font_style_bb .. pretty_all_done .. "[/SIZE][/FONT]"; pretty_all_done = pretty_all_done .. "[SIZE='1']output enhanced with [URL=http://www.indigorose.com/forums/showthread.php?t=19409]AMS Code Pretty[/URL][/SIZE]\n\n\n"; end return pretty_all_done; end function pretty_xml_tag_left(in_str) if (in_str == nil) then return ""; end local out_str; if (convert_to_html == true) then out_str = "" .. in_str; if (keyword_italic == true) then out_str = "" .. out_str; end if (keyword_bold == true) then out_str = "" .. out_str; end else out_str = "[COLOR='" .. keyword_color .. "']" .. in_str; if (keyword_italic == true) then out_str = "[i]" .. out_str; end if (keyword_bold == true) then out_str = "[b]" .. out_str; end end return out_str; end function pretty_xml_tag_right(in_str) if (in_str == nil) then return ""; end local out_str; if (convert_to_html == true) then out_str = in_str .. ""; if (keyword_italic == true) then out_str = out_str .. ""; end if (keyword_bold == true) then out_str = out_str .. ""; end else out_str = in_str .. "[/COLOR]"; if (keyword_italic == true) then out_str = out_str .. "[/i]"; end if (keyword_bold == true) then out_str = out_str .. "[/b]"; end end return out_str; end function pretty_lines_xml_file(this_xml) local pretty_all_done = ""; local s_test = ""; for line = 1, Table.Count(this_xml) do s_test = this_xml[line]; s_test = String.Replace(s_test, "<", "@tag_left@"); s_test = String.Replace(s_test, ">", "@tag_right@"); s_test = String.Replace(s_test, "@tag_left@", pretty_xml_tag_left("@tag_left@")); s_test = String.Replace(s_test, "@tag_right@", pretty_xml_tag_right("@tag_right@")); s_test = String.Replace(s_test, "@tag_left@", "<"); s_test = String.Replace(s_test, "@tag_right@", ">"); this_xml[line] = s_test; if (number_lines == true) then this_xml[line] = prefix_lineNbr(line, Table.Count(this_xml)) .. this_xml[line]; if (convert_to_html == false) then this_xml[line] = String.Replace(this_xml[line], " ", ""); end end if (convert_to_html == true) then pretty_all_done = pretty_all_done .. "@tab_double@" .. this_xml[line] .. "
\r\n"; pretty_all_done = String.Replace(pretty_all_done, "\t", pad_tab); pretty_all_done = String.Replace(pretty_all_done, " ", "  "); else pretty_all_done = pretty_all_done .. this_xml[line] .. "\r\n"; end end if (convert_to_html == true) then pretty_all_done = String.Replace(pretty_all_done, "@tab_double@", html_indent); pretty_all_done = html_indent .. font_style .. pretty_all_done .. html_indent .."\r\n"; else pretty_all_done = "[CODE]\r\n" .. pretty_all_done .. "[/CODE]\r\n"; pretty_all_done = font_style_bb .. pretty_all_done .. "[/SIZE][/FONT]"; pretty_all_done = pretty_all_done .. "[SIZE='1']output enhanced with [URL=http://www.indigorose.com/forums/showthread.php?t=19409]AMS Code Pretty[/URL][/SIZE]\n\n\n"; end return pretty_all_done; end function pretty_lines(this_lua) local pretty_all_done = ""; font_style = "\r\n" font_style_bb = "[FONT='Courier New'][SIZE='" .. xlate_FShtmlToBB() .. "']"; html_indent = "\t\t"; in_multiline_comment = false; if (conv_category == "Function List") or (conv_category == "Function List Sorted") then return (pretty_lines_function_list(this_lua)); end if (conv_category == "INI File") then return (pretty_lines_ini_file(this_lua)); end if (conv_category == "XML File") then return (pretty_lines_xml_file(this_lua)); end for line = 1, Table.Count(this_lua) do if (in_multiline_comment == true) then if (String.Find(this_lua[line], str_multiline_comment_end) > 0) then in_multiline_comment = false; this_lua[line] = pretty_comment(this_lua[line]); else this_lua[line] = pretty_comment(this_lua[line]); end else if (String.TrimLeft(String.TrimRight(this_lua[line])) ~= "") then this_lua[line] = pretty_one_line(this_lua[line]); if (String.Find(this_lua[line], str_multiline_comment_start) > 0) then in_multiline_comment = true; end end end if (number_lines == true) then this_lua[line] = prefix_lineNbr(line, Table.Count(this_lua)) .. this_lua[line]; if (convert_to_html == false) then this_lua[line] = String.Replace(this_lua[line], " ", ""); end end if (convert_to_html == true) then pretty_all_done = pretty_all_done .. "@tab_double@" .. this_lua[line] .. "
\r\n"; pretty_all_done = String.Replace(pretty_all_done, "\t", pad_tab); pretty_all_done = String.Replace(pretty_all_done, " ", "  "); else pretty_all_done = pretty_all_done .. this_lua[line] .. "\r\n"; end end if (convert_to_html == true) then pretty_all_done = String.Replace(pretty_all_done, "@tab_double@", html_indent); pretty_all_done = html_indent .. font_style .. pretty_all_done .. html_indent .."
\r\n"; else pretty_all_done = "[CODE]\r\n" .. pretty_all_done .. "[/CODE]\r\n"; pretty_all_done = font_style_bb .. pretty_all_done .. "[/SIZE][/FONT]"; pretty_all_done = pretty_all_done .. "[SIZE='1']output enhanced with [URL=http://www.indigorose.com/forums/showthread.php?t=19409]AMS Code Pretty[/URL][/SIZE]\n\n\n"; end return pretty_all_done; end