Andromo - Start Making Apps - Free Signup

Announcement

Collapse
No announcement yet.

FREE UTILITY: AMS Code Pretty

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • FREE UTILITY: AMS Code Pretty

    A while back I posted a quick function in one of the threads in answer to a request for help. By hand, I highlighted the syntax of the code so that it was easily readable. Maybe you've had a similar experience.

    I then searched for a utility that would do this automatically. When I couldn't find one, I wrote one (in AMS). This file, AMS Code Pretty, is my donation to the AMS community. It was put together over the last four days in my spare time (about 20 hours).

    Once the lexer engine was coded, it was a small step to also provide a functionality to convert AMS/LUA code to HTML. The program does this as well, plus more.

    The following is a sample output. Notice the line numbers; this is one of several user-definable features.

    Code:
    01 
    02 function appPathOriginal() 
    03 	-- returns the original EXE's path ONLY (with trailing \)
    04 	-- this is useful for compressed single-file apps run from a CD
    05 	-- but, it also works with folder-based apps
    06 
    07 	local cdPath = ""; 
    08 	local findMarker = "SFXSOURCE:"; 
    09 	local cmdArgs = ""; 
    10 
    11 	if (Table.Count(_CommandLineArgs) > 0) then 
    12 		for i = 1, Table.Count(_CommandLineArgs) do 
    13 			if (String.Find(_CommandLineArgs[i], findMarker) > 0) then 
    14 				cmdArgs = _CommandLineArgs[i]; 
    15 				break; 
    16 			end 
    17 		end 
    18 	end 
    19 
    20 	if (cmdArgs ~= "") then 
    21 		if (String.Find(cmdArgs, findMarker) == 1) then 
    22 			local fullPath = String.Replace(cmdArgs, findMarker, "", true); 
    23 			local tblFullPath = String.SplitPath(fullPath); 
    24 			cd_Path = tblFullPath.Drive .. tblFullPath.Folder; 
    25 		end 
    26 	else 
    27 		cd_Path = _SourceFolder .. "\\";"
    28 	end 
    29 	return cd_Path; 
    30 end 
    31


    The download installs and starts it for you. I'm interested to know if anyone else finds this utility useful. Questions, comments, and suggestions are welcome.



    AMS_Code_Pretty_1.0.3_Install.exe

  • #2
    Hey, nice work, :yes :yes :yes

    this is something i can make good use of, and i urge anyone that posts code in the forum to use this well handy little tool


    Nice Work CyberRBT, And Thanks
    Embrace change in your life, you never know, it could all work out for the best

    Comment


    • #3
      Thank you. There is a LOT of code in the posts here. Maybe a utility like this will help make it easier to follow.

      I have discovered a minor bug in the lexer engine. It is now fixed. Before, if you had two quoted backslashes alone, the output added an extra quote.

      The current version is 1.0.3.2. If you were one of the two dozen or so who DL'd right after the above link was posted, you may want to grab this latest update. When you do, make sure that the older version is not running (exit from the system tray) and you may also want to uninstall it (not required though).

      The link above is valid for the latest version.

      Line #27 in the source code sample above reveals the minor bug. Now, look at the following which demonstrates the fix.

      Code:
      01 
      02 function appPathOriginal() 
      03 	-- returns the original EXE's path ONLY (with trailing \)
      04 	-- this is useful for compressed single-file apps run from a CD
      05 	-- but, it also works with folder-based apps
      06 
      07 	local cdPath = ""; 
      08 	local findMarker = "SFXSOURCE:"; 
      09 	local cmdArgs = ""; 
      10 
      11 	if (Table.Count(_CommandLineArgs) > 0) then 
      12 		for i = 1, Table.Count(_CommandLineArgs) do 
      13 			if (String.Find(_CommandLineArgs[i], findMarker) > 0) then 
      14 				cmdArgs = _CommandLineArgs[i]; 
      15 				break; 
      16 			end 
      17 		end 
      18 	end 
      19 
      20 	if (cmdArgs ~= "") then 
      21 		if (String.Find(cmdArgs, findMarker) == 1) then 
      22 			local fullPath = String.Replace(cmdArgs, findMarker, "", true); 
      23 			local tblFullPath = String.SplitPath(fullPath); 
      24 			cd_Path = tblFullPath.Drive .. tblFullPath.Folder; 
      25 		end 
      26 	else 
      27 		cd_Path = _SourceFolder .. "\\"; 
      28 	end 
      29 	return cd_Path; 
      30 end 
      31

      Again, the link above is valid for the latest version. And, questions, comments, and suggestions are welcome.

      Comment


      • #4
        thanks for the update :yes

        iv just one small suggestion/request

        when useing the bbcode setting the output is actual bbcode, could you add a button to switch between the actual bbcode and prazed bbocde so we could still see the ams type of code in the preview useing bbcode settings and click the button to see the actual bbocde

        just for visual effect as i always like to check how the output will look b4 posting
        Embrace change in your life, you never know, it could all work out for the best

        Comment


        • #5
          Sure. I can put that feature in the next update.

          But, for now you can preview as HTML to see the results. The visual output for the end-user is the same whether it's HTML or BB Code.

          Comment


          • #6
            Sure is purty

            very nice!

            Comment


            • #7
              Nice App. I like it

              For a future version I wonder if it would be possible to insert the proper indenting? It drives me nuts when code is not indented at if, for statements.

              Adam Kapilik

              Comment


              • #8
                I agree, it makes the code so much easier to read.

                Comment


                • #9
                  But, for now you can preview as HTML to see the results. The visual output for the end-user is the same whether it's HTML or BB Code.
                  yeah good point, but how about putting the checkbox for the output setting on the main interface, that way i could view as html and quickly view as bbcode without haveing to go in to the settings, that would proberly be easyer that way

                  I agree, it makes the code so much easier to read.
                  ditto, i only recently learned the importance of indenting my code, also i only just found that i could put small blocks on one line :

                  Code:
                  01 if something then dosomething else dont_dosomething end
                  makes things look a lot more tidy
                  Embrace change in your life, you never know, it could all work out for the best

                  Comment


                  • #10
                    You're getting feedback, suggestions. Good sign. :yes

                    Intrigued

                    Comment


                    • #11
                      Intrigued:
                      You're getting feedback, suggestions. Good sign.
                      Hey. This post went live today around 10:30 a.m.; it took about a day for the staff to approve it. And ... i've had 37 DL's in first few hours with 100 views here. Thanks, one and all, for trying it ... and for your suggestions.

                      RizlaUK:
                      ...but how about putting the checkbox for the output setting on the main interface...
                      That is exactly how I envisioned doing what you'd suggested earlier. That update will be available soon.

                      Adam:
                      For a future version I wonder if it would be possible to insert the proper indenting? It drives me nuts when code is not indented at if, for statements.
                      Programming keeps my in touch with my inner Type-A. These days, I'm a fanatic about indenting blocks ... and about naming conventions ... all Label names begin with 'lbl_', all ComboBox names begin with 'cbo_', etc. I also comment liberally (after debuging). Over the years, I've learned (the hard way) that following these guidelines makes updating later SO much easier.

                      Nonetheless, with the lexer engine the focus is on isolating keywords and syntax, then highlighting them accordingly. Indention, like naming conventions, is a choice of style. Although, I may add an indention feature, at this juncture I'm not quite sure how to properly handle nested blocks.

                      Comment


                      • #12
                        How do you feel about sharing an exported version of this utility (.apz)?
                        Intrigued

                        Comment


                        • #13
                          Well ... the project is still in development. But, the help file does give clues on how to get the source code. Nonetheless, I wouldn't want the lexer engine source posted at this time. However, clever programmers (such as yourself Intrigued) could acquire it and use it in their own projects (as long as the AMS lexer engine source is not disclosed).

                          BTW, writing this type of code using regular expressions and a RichEdit control takes a few dozen lines of code. Using AMS to syntax highlight AMS code took over 300 lines.

                          Here is a list of the functions in the latest update. You can use these prototypes after you load the bytecode compiled script (code_pretty.luac). The parameter for the 'pretty_lines()' function is a table of source code lines.

                          Code:
                          function isCharAlpha(char_test) 
                          function isKeyword(str_test) 
                          function prefix_lineNbr(nbr, maxlines) 
                          function prep_punctuation_all(in_str)  -- the easy punctuation
                          function pretty_comment(in_str) 
                          function pretty_keyword(in_str) 
                          function pretty_lines(this_lua) 
                          function pretty_lines_function_list(this_lua) 
                          function pretty_lines_ini_file(this_ini) 
                          function pretty_lines_xml_file(this_xml) 
                          function pretty_one_line(str_in)  -- the big one
                          function pretty_punctuation(in_str) 
                          function pretty_quoted(in_str) 
                          function pretty_xml_tag_left(in_str) 
                          function pretty_xml_tag_right(in_str) 
                          function sort_no_case(v1, v2) 
                          function StringToTable(DelimitedString, Delimiter) 
                          function xlate_FShtmlToBB()

                          BTW, alphabetic function list output as displayed (to HTML or BB Code) is a new feature.

                          And, here are the global variables required by the engine:

                          Code:
                          function restore_defaults() 
                          	convert_to_html = true; 
                          	html_fontsize = "9"; 
                          	number_lines = true; 
                          	
                          	comment_color = "#008000"; 
                          	comment_italic = true; 
                          	comment_bold = false; 
                          	
                          	quoted_color = "#800080"; 
                          	quoted_italic = false; 
                          	quoted_bold = false; 
                          	
                          	keyword_color = "#0000FF"; 
                          	keyword_italic = false; 
                          	keyword_bold = true; 
                          	
                          	punctuation_color = "#FF0000"; 
                          	punctuation_italic = false; 
                          	punctuation_bold = false; 
                          
                          	settings_save();	 
                          end

                          Comment


                          • #14
                            Okay. Here is another update. Now i'm going to wait for a while before updating the application while collecting a list of suggested improvements. Help / About apprises you of your installed version number.

                            The link in the intial post will DL the latest version.

                            The current version is now 1.0.3.4. When you install this update, make sure that the older version is not running (exit from the system tray) and you may also want to uninstall it (not required though).

                            Since AMS supports INI and XML files, I added a feature to open them and syntax highlight to HTML or BB Code. The HTML conversion is good for placing in the BODY of a Web page or in a help file.

                            Here are the updates:

                            1.0.3.4 (2007-04-03)

                            - HTML font sizes now alter BB Code too: HTML '7' is BB 1, HTML '8' is BB 2, HTML '9' is BB 3 (the defaults), HTML '10' is BB 4, and so on.

                            - New feature. In addition to conversion styles (HTML or BB Code), conversion types are now supported. They include: (1) All LUA Code,
                            (2) Function List, (3) Function List Sorted, (4) INI File, and (5) XML File.

                            1.0.3.3 (2007-04-02)

                            - enhanced the visual contrast between active and inactive wizard tabs

                            - added an HTML checkbox to the “Output Preview” page (suggested by RizlaUK above)

                            - implemented minor design enhancements to the “Output Settings” page

                            - to the “Help” page added Reference Sections: “AMS Sites” and “Coding Style” (the Coding Style topic was broached by Adam above)

                            Comment


                            • #15
                              BTW, writing this type of code using regular expressions and a RichEdit control takes a few dozen lines of code. Using AMS to syntax highlight AMS code took over 300 lines.
                              These may come in handy, as I noticed your comment:

                              http://lua-users.org/wiki/ExpressionsTutorial
                              http://lua-users.org/wiki/StringInterpolation
                              http://lua-users.org/wiki/StringLibraryTutorial


                              Thanks for sharing the parts you did. I like to see how others are coding. This helps me to make sure I'm coding the best I can and to find some new insights, tips that others are using.

                              :yes
                              Last edited by Intrigued; 04-03-2007, 04:50 PM.
                              Intrigued

                              Comment

                              Working...
                              X