Wiki Home

Wiki Java Beautify


Namespace: VFP


Wiki Java Beautify


I've started a Javascript project that will provide powerful features to an already powerful program - the Wiki. Please feel free to jump in and help (at any capacity) --->The Kid<---
2002-05-24 Added Clipboard() Java function Example follows:


*- Program to display Hello World
PROCEDURE Helloworld
    MESSAGEBOX("Hello World,0,"Copy To Clipboard")
ENDPROC

*- There is a java Clipboard() function now!

--->TheKid<---


There are a few, very basic, formatting rules to use the Javascript code (they will become quickly evident when you Edit this document). To simplify matters I wrote the below program that will do the formatting for you; all you have to do is copy and paste, run the program, select the program to convert, and it will place it in the clipboard for you formatted as required for the java code to display.

Note: One formatting rule, not so readily apparent, is with HTML tags. It will display tags with a space < FONT> as follows: .

Feel free to use, modify, steal the code; if you can help me improve on it (there is more to come as I update our Intranet) please drop me an email. --->The Kid<---
What's the difference between this (apparantly fairly convoluted) method and using < pre > tags (see below)?

Note that I did have to fiddle a little. I used spaces in the < script > comment and changed the VFP code to
'<' + 'script>'

and
'<' + '/script>'

in PROCEDURE m_CopyToClipBoard

-- Andrew Coates
BECAUSE I HAVE TO MUCH TIME ON MY HANDS :) Not really, this is a good question - helps emphasize the value of this java script; I would like help making it better even if the help were in just bug reports.

An evolution process - It started with just a java Tag() command that would permit me to properly display tags in my text, I then ran into issues with a LTRIM() effect and before I knew it I had Output() written. It was fairly convoluted, as you suggested, so I wrote the following application. When I posted this message I had lost sight of my original intentions and ended up posting an overglorified command - or so it would appear....

We use the Wiki in our organization and some of our developers are not well versed in HTML so I post snippits of code with tips and tricks - e.g., to display an image to the screen store it in the \\server\images\ area. In your html source input .

The above paragraph is how I want to format my text so I can make my message clear.
I can't do the above with the tags. Note how I can have
on the same line with my text. Perhaps more importantly they can cut and paste any code without
having to fix tags.

--->The Kid<---


< pre > Method:
#DEFINE DEBUG_MODE .T. && True will show messagebox in error handler

LOCAL oWiki

oWiki = CREATEOBJECT('oWikiFormat',GETFILE('PRG'))

IF TYPE('oWiki')='O' AND !ISNULL(oWiki)
     WITH oWiki
         IF .pl_ErrFlg
             RETURN MESSAGEBOX(.pc_ErrMsg,16,'Aborted')
         ENDIF
         oWiki.m_CopyToClipBoard()
         MESSAGEBOX('Data is in clipboard!',64,'Wiki Convert')
     ENDWITH
ENDIF


*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*
*::::::::::::::::::::::::[ CLASS DEFINITION ]:::::::::::::::::::::::::::*
*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*

DEFINE CLASS oWikiFormat AS CUSTOM
     *) Purpose: Class to handle Beautify conversion of file to
     *) format that Wiki Java Output() command will support
     *)
     *) Properties:
     *) pl_ErrFlg = .F. && True if there was an error
     *) pc_ErrMsg = '' && Error message
     *) pc_FileData = '' && Contents of imported file
     *) pn_NumLines = 0 && Number of lines to process
     *) pn_MaxLen = 0 && Maximum line length - we use for padr()
     *)
     *) *- The following properties programmatically added in INIT
     *) pa_FileData(1) && Array with file data
     *) pa_FileSpaces(x) && Holds number of spaces on the FOLLOWING line
     *)
     pl_ErrFlg = .F.
     pc_ErrMsg = ''
     pc_FileData = ''
     pn_NumLines = 0
     pn_MaxLen = 0

     PROCEDURE INIT(tcFilename)
         *) Purpose: Convert tcFilename to string (and array) for processing
         *) by class methods.
         *)
         *) Parameters:
         *) tcFilename .... Filename to convert to Wiki Java format
         *)
         LOCAL lnOffset
         WITH THIS
             DO CASE
                     *- Don't instantiate if no file selected
                 CASE EMPTY(tcFilename)
                     RETURN .F.
                     *- Instantiate if file not found so we can
                     *- trap and display error in calling program
                 CASE !FILE(tcFilename)
                     .m_SetError(tcFilename+' was not found!')
                     RETURN

                     *- All other errors handled by error method
             ENDCASE
             *- Convert the file to a string and store in object
             .pc_FileData = FILETOSTR(tcFilename)

             *- Get the number of lines and convert to array
             *- for processing by class
             .ADDPROPERTY('pa_FileData(1)')
             .pn_NumLines = ALINES(.pa_FileData,.pc_FileData)

             *- Preprocess each line
             FOR lni = 1 TO .pn_NumLines
                 *- Convert any tabs to spaces
                 lnOffset = AT(CHR(9),.pa_FileData(lni))
                 DO WHILE lnOffset > 0
                     .pa_FileData(lni) = STUFF(.pa_FileData(lni),lnOffset,1,SPACE(4))
                     lnOffset = AT(CHR(9),.pa_FileData(lni))
                 ENDDO
                 *- Get the length of the longest line
                 .pn_MaxLen = MAX(.pn_MaxLen,LEN(TRIM(.pa_FileData(lni))))
             NEXT

             *- Create array that will hold number of spaces that
             *- preceed the next line so we can add the chr(13)+spaces(x)
             *- statement.
             .ADDPROPERTY('pa_FileSpaces('+ALLTRIM(STR(.pn_NumLines))+')',0)
             .m_SetSpaces() && Set .pa_FileSpaces array
             *- Class is initialized, we're out'a here.
         ENDWITH
     ENDPROC

     PROC ERROR(tnError, tcMethod, tnLine)
         *) Purpose: Set class error state so calling process
         *) can trap and display.
         *) Parameters:
         *) tnError .... Error Number
         *) tcMethod ... Method that generated error
         *) tnLine ..... Line number with error
         *)
         *) Returns: TRUE
         *)
         WITH THIS
             .m_SetError('Error number: ' + .m_trimCR(tnError)+;
                 'Error message: ' + .m_trimCR(MESSAGE())+;
                 'Message(1): ' + .m_trimCR(MESSAGE(1))+;
                 'Program: ' + .m_trimCR(SYS(16,1))+;
                 'Method: ' + .m_trimCR(tcMethod)+;
                 'Line: ' + .m_trimCR(tnLine))

             *- Will only trap for errors during development. All errors
             IF VERSION(2)#0
                 *- We already trap and return on Init errors
                 IF LOWER(tcMethod)='init'
                     RETURN
                 ENDIF
                 *- There are no messagebox commands in this class. This is useful
                 *- when modifying class code so you don't blow by errors.
                 IF MESSAGEBOX(.pc_ErrMsg+CHR(13)+CHR(13)+'Suspend?',16+4,tcMethod)=6
                     SET STEP ON
                 ENDIF
             ENDIF
         ENDWITH
     ENDPROC

     PROCEDURE m_CopyToClipBoard
         LOCAL lcClipText
         WITH THIS
             lcClipText = ''
             FOR lni = 1 TO .pn_NumLines
                 lcClipText=lcClipText + .m_GetWikiStr(.pa_FileData(lni),lni)
             NEXT
             *- The lcClipText will have a < script > command that
             *- will interfere during the paste. We'll fix it here
             lcClipText = .m_FixTag(lcClipText,'script')
             lcClipText = .m_FixTag(lcClipText,'/script')
             *- Put it the clipboard
             _CLIPTEXT = '<' + 'script>lcWikiVFP=[]+'+CHR(13)+;
                 lcClipText +;
                 'Output(lcWikiVFP);'+CHR(13)+;
                 '<' + '/script>'
         ENDWITH
     ENDPROC

     PROCEDURE m_FixTag(tcData,tcTag)
         LOCAL lcSrchFor
         lcRetVal = tcData
         lcSrchFor = '<'+tcTag
         lnOffset = AT(lcSrchFor,lcRetVal)
         DO WHILE lnOffset > 0
             lcRetVal = STUFF(lcRetVal, lnOffset, LEN(lcSrchFor),'<'+tcTag)
             lnOffset = AT(lcSrchFor,lcRetVal)
         ENDDO
         RETURN lcRetVal
     ENDPROC

     PROC m_GetWikiStr(tcRawStr,tnOffset)
         *) Purpose: Converts any double quotes to single quotes
         *) and formats character string as required to
         *) support Wiki Java's Output() command
         *)
         *) Parameters:
         *) tcRawStr .... String to convert
         *)
         *) Returns: Padded string supporting Wiki Java Output() format
         *)
         WITH THIS
             *- Convert any double quotes to single quotes
             lcRetVal = CHRTRAN(tcRawStr,CHR(34),CHR(39))

             lcRetVal = CHRTRAN(lcRetVal,' ',chr(160))

             IF TYPE('tnOffset')='N' AND tnOffset # .pn_NumLines
                 *- Add the chr(13) and spaces(x) statement to the line
                 lcRetVal = CHR(34) + PADR(lcRetVal,.pn_MaxLen)+CHR(34)+;
                     '+CHR(13)+'+IIF(.pa_FileSpaces(tnOffset)>0,'SPACES(' +;
                     ALLTRIM(STR(.pa_FileSpaces(tnOffset)))+')+','')
             ELSE
                 *- Add the chr(13) and spaces(x) statement to the line
                 lcRetVal = CHR(34) + PADR(lcRetVal,.pn_MaxLen)+CHR(34)+;
                     IIF(tnOffset#.pn_NumLines,'+CHR(13)+',' ')

             ENDIF
         ENDWITH
         RETURN lcRetVal + CHR(13)+CHR(10)
     ENDPROC

     PROC m_SetSpaces()
         *) Purpose: Determine the number of spaces that preceed the
         *) *next* line so a SPACE(numlines) statement can
         *) be appended to this line AFTER the chr(13) statement.
         *)
         *)
         WITH THIS
             LOCAL lnOffset,lni
             lnOffset = 0
             *- We step backwards through each line
             FOR lni = .pn_NumLines TO 1 STEP -1
                 .pa_FileSpaces(lni)=lnOffset
                 *- We subtract the length of the string from the length
                 *- of the same string with a ltrim() applied - the result
                 *- equals the number of spaces preceeding the line
                 lnOffset = LEN(.pa_FileData(lni))-LEN(LTRIM(.pa_FileData(lni)))
             NEXT
         ENDWITH
     ENDPROC

     PROCEDURE m_trimCR(tuTrim)
         *) Purpose: Perform a ALLTRIM() and add a CR to tuTrim
         *) where tuTrim can be a character or numeric value
         *)
         *) Parameter:
         *) tuTrim .... string/value to ALLTRIM
         *)
         *) Returns: ALLTRIM(tuTrim) with a CR/LF
         *)
         DO CASE
             CASE TYPE('tuTrim')='C'
                 RETURN ALLTRIM(tuTrim)+CHR(13)+CHR(10)
             CASE TYPE('tuTrim')='N'
                 RETURN ALLTRIM(STR(tuTrim))+CHR(13)+CHR(10)
         ENDCASE
     ENDPROC

     PROCEDURE m_SetError(tcMsg)
         *) Purpose: Sets class error status
         *)
         *) Returns: FALSE so you can set and force abort with
         *) one statement
         *)
         THIS.pl_ErrFlg = .T.
         THIS.pc_ErrMsg = tcMsg
         RETURN .F.
     ENDPROC

     PROCEDURE m_IsError()
         *) Purpose: Returns TRUE if there was an error
         *)
         RETURN THIS.pl_ErrFlg
     ENDPROC
ENDDEFINE


Category Tips And Tricks
Category Wiki Tools
[http://www.j-cellular.com unlocked cell phones]
( Topic last updated: 2011.05.26 12:08:33 PM )