Sunday, March 1, 2009

VFP Syntax Coloring in Blogger

So I'm all excited about the prospect of starting a blog and I've got a list of topics going that I keep adding to. I'm working on one the other night (not published yet) and I wanted to add a few source code examples to it.  Having never done this before, I'm thinking to myself, this shouldn't be that big of a deal, right?  Wrong.  

I'm publishing this blog with Google's free Blogger service.  Say what you want about this tool but it was super easy to get started and with a last name like Krzystek, I didn't have to worry about the availability of wkrzystek on blogspot.com (or any other domain for that matter!).  It's pretty customizable with several templates to choose from, many gadgets you can use, and more.  If you are thinking about starting a blog and you are not sure which to use, I recommend this one very highly based on my experience so far.  Many others tell me to upgrade to WordPress and it seems pretty cool as well but this was the one I had picked and I'm not interested in moving it at this juncture unless I find a compelling reason to do so. 

One of the first items I tried was simply pasting my code directly into the Blogger editor and when I did this, the formatting was fine but I lost all of the Syntax coloring. Hmm.  I've seen this before and I've used Word as a middle step so I launched a new instance of Word, pasted my VFP code and everything looked good.  However, when I pasted the code in Blogger, the syntax coloring came across properly but everything was double spaced.  Further more, trying to clean this formatting up in the Blogger editor was a major PITA to get the code to look like it was in VFP.  This is too much work for my taste.  

It seemed like Word was putting out CRLF's at the end of each line and then Blogger was converting each CR and LF to an Html line break tag.  OK, I can handle this, back to VFP again - _Cliptext = STRTRAN(_Cliptext, CHR(10), "").  When I pasted this into blogger, everything was formatted properly again but all of my syntax coloring was now gone.  Doh. Of course this made immediate sense to me since _Cliptext = _Cliptext is a tip I use every day in VFP (intellisense: CP2) to clear any formatting from the clipboard before pasting into another source where I don't want the original formatting to come along (this is great when copying stuff from the web into word or other rich text editors).

At this point, I started digging deeper into the problem.  I had remembered a recent blog post from Bernard Bout who referenced a blog by Burkhard Stiller who was working on a project to convert the RTF used by VFP into valid HTML snippets so that it's easier to use when blogging.  This seemed to me to be exactly what I needed.  I downloaded the last copy from Burkhard's blog and tried running the form (OK, I should have spent more time reading his 7-part blog post first but that's just not me.)  As soon as I ran the form, it crashed and after a little debugging, I determined that the clipboard format that my VFP is putting out wasn't the same as what Burkhard was expecting.  OK, now it's time to read his posts to see if I can figure this out.  Burkhard was using a constant of 49298 for the VFP clipboard RTF output and this value returned an empty string when I run the form.  On his blog in Part 5 he shows some VFP code using FoxTools to enumerate all of the info on the clipboard.  I modified this slightly for my use just to save the clipboard contents to a file that I could inspect in UltraEdit:

SET LIBRARY TO HOME() + "Foxtools.fll" ADDITIVE
IF
OpenClip(_SCREEN.HWND)
           
ACTIVATE SCREEN
            CLEAR
           
*\\ Start with EnumClipFm(0) gets the first format available.
           
nFormat = EnumClipFm(0)
           
DO WHILE m.nFormat != 0
                       
IF BETWEEN(m.nFormat, 49152, 65535)
                                    ??
" | GetClipFmt(#" + ;
                                               
TRANSFORM(m.nFormat) + ") =", ;
                                               
GetClipFmt(m.nFormat)
                       
ELSE
                                   
?? " | GetClipFmt(#" + ;
                                               
TRANSFORM(m.nFormat) + ") =", ;
                                               
"standard-format"
                       
ENDIF

                       
*** WTK: Save the clipboard text to a file
                        *** for further inspection
                       
lcClipText = GetClipDat(m.nFormat)
                       
STRTOFILE(lcClipText, "cp_" + ;
                                               
TRANSFORM(m.nFormat) + ".txt", 0)

                       
nFormat = EnumClipFm(m.nFormat)
           
ENDDO
           
= CloseClip()
ENDIF

When I ran this code, the VFP RTF clipboard data was saved to cp_49292.txt.  OK, go back to the form, change this value and voila - I get the HTML formatted code shown properly in the embedded browser control on the form.   OK.  This wasn't too bad.  To get the html generated, I simply right-clicked and chose View Source from the browser control and I was able to get the html snippet.  (As a side note, EditPadPro is an awesome html View Source utility.  It automagically sets itself to be the View Source app for IE on install and displays the HTML in the same syntax coloring as Visual Studio's web developer).  When I pasted the html in the blogger editor, it looked simply awful (as html does to the naked eye) and further more when you previewed the output of the post, all of the coloring was gone and none of the indenting was retained.  In blogger, you have both a rich editor control as well as the ability to tweak the html source directly.  I attempted to paste the generated HTML directly into the html source window and this still didn't work.  Why not?  Well, the way Burkhart's tool works currently is that he is using a CSS style for each style of code formatting options in VFP (strings, literals, commands, comments, etc).  Since blogger is all template based, these CSS values are never registered. At this point I should have punted, but no - I'm much more stubborn than that.  I went into the blogger layout options and tried in vain to add the CSS styles from the generated HTML code to the tempate's Head section.  I made several different attempts at this to no avail and when I evaluated the risk of permanently fouling up my Blogger template if I continued I decided this was not the way to go.

I then went out to see if I could figure out what others were doing.  I remembered reading a post from Rick Strahl's Blog about how he was getting syntax coloring for ASP.NET code from Visual Studio.  While very cool, this wouldn't work for me. I then checked Doug Hennig's Blog, Rick Shummer's Blog and Cathy Pountney's Blog who were all using Blogger and saw that they didn't have source code syntax coloring in their blogs at all.  I asked myself, can this be done?  At this point I was getting tired and just a little frustrated and feeling like my concept of short and frequent blog posts had all gone to heck since I had been at this for a couple of hours now and I stopped for the night.

The next morning, I tweeted about the experience and had a few suggest tweaking my _VFP.EditorOptions to either include X or make sure that it was excluded.  My setting was "LQKWT" and X wasn't included.  When I looked at the help file since this isn't a setting I'm often tweaking, I saw that the purpose of X was specifically to suppress the RTF clipboard formatting.  This wasn't it.  I started going back to the thought of using Word as an intermediary which was also suggested.  If you remember from the top of this post, when I pasted from Word, everything was double spaced.  I couldn't tweak the clipboard externally from VFP but could I affect this from within Word directly?  One of the things I always have set in Word is to Show all non-printing characters - I want to know if there is a tab character or if someone held down the space bar for 5 seconds to indent (that drives me nuts).  When I pasted the code, I saw that each line had a end of paragraph symbol at the end of the line (what you get when you press the Enter key).  I thought about changing these all to a "soft-enter" or "line-break" (what you get when you press Shift+Enter).  One could certainly do this manually but that would be a pain for large blocks of code.  I'm unfortunately using Word 2007 (which I still haven't acclimated to after about 6 months) so I'm not sure exactly if this tip will work in earlier or later versions of Word but I suspect that they will. I pasted my VFP code into a new Word document so I could safely do a Replace All.  I launched the Find and Replace dialog and in my Find I put "^p" to search for a Carriage return/paragraph mark and in the Replace I put "^l" to replace with a New line.  Check out this page for Special characters you can use with Find and Replace in Word.  This worked like a charm - the code came into blogger properly indented and formatted and with the same syntax coloring I have in VFP.  

To summarize what I learned in this process is that you really can't get the VFP syntax coloring copied out to anywhere but Word with any sense of reliability.  To export it from Word to the Blogger editor, first replace all paragraph marks (^p) with new lines (^l) and then you can copy from Word directly into Blogger and all is well.  I'm sure that if I tried hard enough, I could probably write a Word automation utility that would do all of this for free.  That would be kinda cool, wouldn't it?  Sounds like a future blog post!