Wiki Home

Wiki Beautify


Namespace: WIN_COM_API
This sounds pretty cool, but I think most people would prefer to cut and paste code and run it than have to edit it first. Maybe a better idea would to have the wiki parser automatically have lines that start with an * and are enclosed in a < pre > tag displayed in green. That would automatically highlight comments and still produce executable code. -- Mike Feltman
This program will format code for pasting into the wiki editor window. Just put the code in the clipboard buffer, run this program, then paste into wiki. It makes a smaller font and colors comments green.

The problem in using it is that the code sample can't then be copied to a VFP PRG file and run!

However, it does not work properly yet. I need some help.

I can't figure out when and why wiki puts a question mark after something.
Mark, why does your code say "< pre > < /pre >". This is screwing up the parser. It makes no sense to open and close tags like that. Before I devote time fixing this highly improbably tag syntax, why are you doing it?-- Steven Black
Hi Steve--thanks for your help. I found that if I bracket the code in two pre/endpre tags, then the triple-click function "select-all" still works. (ul/endul does the same thing.) I changed the prg to put some text between the pre/endpre, so you don't need to mess with the parser to deal with this.

I tried avoiding this by using the NOHOTlinks directive, but this doesn't appear to be working, so you will see question marks sprinkled through the code. I found I could get rid of some of the question marks by changing replacing the non-breaking space with a plain old space after words like "PROCEDURE" and "LPARAMETERS". I also found that if I made the word lower case, the question mark went away.

Finally, note that you cannot edit this "beautified" code in the wiki edit page. Wiki changes stuff (like the font color directives) after you save it. You have so copy the code (from the rendered HTML), edit it in VFP, run the program against the edited code and paste the whole thing back into the edit window.

Maybe people won't find this so useful--but I had fun writing this and I think the output is much improved. I started thinking about further formatting like putting class names in normal bold and underlining procedure names. Wouldn't that be cool!

NB: If you are working on this utility, please put in the under contruction icon!

Who ever is working on this, I started something like this independantly, look at http://24.14.98.235/mhhtmlcodetest.htm

There is a file you can download that inlcudes the nescarry table at http://24.14.98.235/ under Files, then mhHTMLCode. Mike Helland
NN Mike, your site is really cool, I like it. But the file is quite old (3/5/00) - your first version. Could you please update it?

There is a new web version of this at http://24.14.98.235/htmlcode.asp -- Mike Helland - NN it doesn't contain file for download either.
**/
* Takes text in clipboard, formats it for pasting on wiki and puts it back in the clipboard.
*
* @Examples
*           1. Select the entire text of a program
*              Press Control-C
*              Do WikiBeautify
*              The HTML is now in the clipboard ready for pasting onto Wiki
*
*           2. To edit code already on the wiki, put into the clipboard
*              from the rendered HTML, NOT the edit mode.  The edit mode
*              takes away things like < , " and so on.
*
*           3. Open two instances of VFP.  Have the wikiBeautify
*              program active in one, and use the other instance to put
*              into the clipboard.  Then it's Ctrl-C, Alt-Tab, Ctrl-E,
*              Alt-Tab (to wiki), Ctrl-V.
*
* @Author  Mark Bucciarelli 27-Jan-00
* @Version 2
**/

#DEFINE ID_CR              CHR(13)
#DEFINE COMMENT_PRE         "  "
#DEFINE COMMENT_POST      " "
#DEFINE SPACES_PER_TAB      3

PUBLIC gcErr, gnDebugMode

* 0 = no output from MyError() / 1 = echo to screen / 2 = set step on

gnDebugMode = 0
gcErr = ""

LPARAMETERS loSettings, lcS, lcOldClipText

* Set up environment

loSettings = CREATEOBJECT("Environment")

ON ERROR DO myError WITH LINENO()

* Do the work ...

lcS = _CLIPTEXT
lcOldClipText = lcS

* Order is important here; e.g., don't replace spaces until
* after ColorComment function is called.  Also, don't add
* tags before < and > strtran().

lcS = STRTRAN(lcS, '"', "&" + "quot;")
lcS = STRTRAN(lcS, "<", "&" + "lt;")
lcS = STRTRAN(lcS, ">", "&" + "gt;")
lcS = ColorComments(lcS)
lcS = " " + lcS
lcS = STRTRAN(lcS, ID_CR, " " + ID_CR + " ")
lcS = STRTRAN(lcS, CHR(9), REPLICATE("&" + "nbsp;", SPACES_PER_TAB))
lcS = STRTRAN(lcS, " ", "&" + "nbsp;")
lcS = ID_CR + "<" + "nowiki>" + ID_CR + "<" + "pre" + ">------- Start of included code -------" + "<" + "/pre>" + ID_CR + lcS
lcS = lcS + ID_CR + "<" + "pre>"
lcS = lcS + "------- End of included code -------<" + "/pre>" + ID_CR
lcS = lcS + "<" + "/nowiki" + ">" + ID_CR

* For some reason, wiki turns "PROCEDURE MyProc" into
* PROCEDURE ?;MyProc.  Same for LPARAMETERS and LOCAL.
* If you replace the non breaking space with a regular
* space it works fine.

lcS = STRTRAN(lcS, "PROCEDURE" + "&"+ "nbsp;", "PROCEDURE ")
lcS = STRTRAN(lcS, "LPARAMETERS" + "&" + "nbsp;", "LPARAMETERS ")
lcS = STRTRAN(lcS, "LOCAL" + "&" + "nbsp;", "LPARAMETERS ")
_CLIPTEXT = lcS

loSettings.RESET()

IF !EMPTY(gcErr)
  MESSAGEBOX(gcErr)
  _CLIPTEXT = lcOldClipText
ENDIF


**/
* Color comment text.   Doesn't do in-line comments.
**/
FUNCTION ColorComments
  LPARAMETER tcS
  LPARAMETERS lcRest, lcS, lnPos, lnCr

  lcRest = tcS
  lcS = ""
  DO WHILE EMPTY(gcErr) AND AT("*",lcRest) > 0
    lnPos = AT("*", lcRest)

    * If only whitespace between the last carriage
    * feed and this asterisk then this is a comment.

    lnCr = RAT(ID_CR,LEFT(lcRest,lnPos))
    lcTest = SUBSTR(lcRest, lnCr + 1, lnPos - lnCr - 1)
    lcTest = STRTRAN(STRTRAN(lcTest," ", ""),CHR(9),"")
    IF EMPTY(lcTest)
      lcS = lcS + LEFT(lcRest, lnCr) + COMMENT_PRE
      lcRest = SUBSTR(lcRest, lnCr + 1)

      * Find end of comment line.

      lnCr = AT(ID_CR, lcRest)
      lcS = lcS + LEFT(lcRest, lnCr - 1) + COMMENT_POST

      * Leave terminating CR in case next line is a comment
      * too.

      lcRest = SUBSTR(lcRest, lnCr)
    ELSE
      lcS = lcS + LEFT(lcRest,lnPos)
      lcRest = SUBSTR(lcRest, lnPos + 1)
    ENDIF
  ENDDO
  lcS = lcS + lcRest
  RETURN lcS
ENDFUNC


**/
* Load global variable gcErr with first error encountered
**/
PROCEDURE myError
  LPARAMETER tnLine
  LPARAMETERS lcMsg

  lcMsg = MESSAGE()

  IF EMPTY(gcErr)
    DO CASE
      CASE gnDebugMode = 0
        gcErr = lcMsg
      CASE gnDebugMode = 1
        gcErr = "Error at line #" + LTRIM(STR(tnLine))+ ": " + lcMsg
        ACTIVATE SCREEN
        ?gcErr
      CASE gnDebugMode = 2
        gcErr = "Error at line #" + LTRIM(STR(tnLine))+ ": " + lcMsg
        ACTIVATE SCREEN
        ?gcErr
        SET STEP ON
      OTHERWISE
        SET STEP ON
        * ERROR!!
    ENDCASE
  ENDIF  && EMPTY(gcErr)
ENDPROC

** Class to save and restore environmental settings.

DEFINE CLASS ENVIRONMENT AS CUSTOM
  PROTECTED aTable[1,2]
  PROTECTED aMacro[1,2]
  PROTECTED aTo[1,2]
  PROTECTED cOldError
  PROTECTED nOldSelect

  * The following code works.
  *       a = "vfxfunc.fxp"
  *       SET PROCEDURE TO (a)
  * The following code does not work.
  *       a = "vfxfunc.fxp, applfunc.fxp"
  *       SET PROCEDURE TO (a)
  * So deal with the separately.  I expect SET CLASSLIB will
  * show the same behavior.

  PROTECTED cOldProcedure

  PROCEDURE INIT
    **/
    * Set up arrays and load with current values.
    *
    * MKB 25-Jan-00
    **/
    WITH THIS
      DIMENSION .aMacro[2,2]
      .aMacro[1,1] = "DELETED"
      .aMacro[2,1] = "TALK"
      DIMENSION .aTo[2,2]
      .aTo[1,1] = "PATH"
      .aTo[2,1] = "DEFAULT"
      .SET()
    ENDWITH
  ENDPROC

  * Save current environment

  PROCEDURE SET
    LPARAMETERS ln
    WITH THIS
      =AUSED(.aTable)
      .cOldError = ON("ERROR")
      .nOldSelect = SELECT()
      .cOldProcedure = SET("PROCEDURE")
      FOR ln = 1 TO ALEN(.aMacro,1)
        .aMacro[ln,2] = SET(.aMacro[ln,1])
      ENDFOR
      FOR ln = 1 TO ALEN(.aTo,1)
        .aTo[ln,2] = SET(.aTo[ln,1])
      ENDFOR
    ENDWITH
  ENDPROC

  * Restore values to their defaults

  PROCEDURE RESET
    LPARAMETERS lcCmd, ln, luVal, laTable[1]
    WITH THIS

      * Do data

      =AUSED(laTable)
      FOR ln = 1 TO ALEN(laTable,1)
        IF ASCAN(.aTable, laTable[ln,1]) = 0
          USE IN (laTable[ln,1])
        ENDIF
      ENDFOR

      * Do settings

      FOR ln = 1 TO ALEN(.aMacro,1)
        luVal = .aMacro[ln,2]
        lcCmd = "SET " + .aMacro[ln,1] + " &luVal"
        &lcCmd
      ENDFOR
      FOR ln = 1 TO ALEN(.aTo,1)
        lcCmd = "SET " + .aTo[ln,1] + " TO (.aTo[ln,2])"
        &lcCmd
      ENDFOR
      luVal = .cOldError
      IF EMPTY(luVal)
        ON ERROR
      ELSE
        ON ERROR &luVal
      ENDIF

      luVal = .cOldProcedure
      SET PROCEDURE TO &luVal
      SELECT (.nOldSelect)
    ENDWITH
  ENDPROC
ENDDEFINE


Contributors: MarkBucciarelli, Mike Helland
Version Log
  1. 27-Jan-00/MKB: Written.
  2. 30-Jan-00/MKB: Added text between PRE/ENDPRE. Added WIKI/NOWIKI bracket.

Category Wiki Admin Category Wiki Tools
( Topic last updated: 2000.12.17 05:02:10 PM )