SCN Syntax Highlighting for Dummies
UPDATE: At the end of this blog, you will find updated steps that are able to correct indentation issue.
There are certain limitations to syntax highlighting feature provided by SCN Editor.
ABAP syntax highlighting is not supported, and fixed-width typeface (common for code) can’t be set.
People in ABAP Development space might find it useful
When we copy highlighted code from New ABAP Editor, and paste into SCN Editor, all formatting is lost.
Same limitation applies to highlighted text taken from MS Word document.
However, pasting code from New ABAP Editor to MS Word document retains formatting.
After following the steps, you would be able to paste code with syntax highlighting like this:
* get workitem runtime object
CALL METHOD cl_swf_run_wim_factory=>find_by_wiid
EXPORTING
im_wiid = workitem_id
RECEIVING
re_instance = lv_wi_handle.
Prerequisites
- Your computer has Microsoft Office installed. (these steps were performed using 2007 version)
- Create an empty MS Word document, and save it as Web page, Filtered (*.htm) format.
- Close the document.
- Right-click and Edit option on file would launch MS Word.
Steps to follow
- Copy code snippet from New ABAP Editor.
- Open empty html file (created as a prerequisite). You can open it directly from MS Word or by right-click on file and choosing Edit option.
- Paste code snippet into MS Word html document.
- Copy code snippet from MS Word html document.
- Paste into SCN Editor.
- Close MS Word html document without saving so that same steps can be followed next time.
Added bonus
This method does not rely on buttons present in SCN Editor.
You might have noticed the SCN Editor hides some buttons when you post comment to a blog, like changing font or using advanced features.
Using this method, you would be able to post better snippets even in blog comments.
In addition, same technique might work for colorful text taken from other sources.
P.S.:
You might notice that code indentation is lost. MS Word is somehow stripping the leading blanks.
I’ll update this blog when I find an easy way to fix it.
Modified steps that help retain indentation
It took some hits and trials to find a method that is easy to use by an average user.
Technically, spaces following a line break (<br> tag) are not sent by MS Word to clipboard.
However, spaces following a paragraph break (</p><p>) are sent by MS Word to clipboard.
This is one of the several limitations of MS Word when it acts like an html editor.
So, after pasting code in MS Word htm file, we replace Manual line break with Paragraph mark.
Updated steps (steps 4 to 8 fix indentation) have keyboard shortcuts as they save time. But it can be done using Mouse too with few more clicks.
- Copy code snippet from New ABAP Editor.
- Open empty html file (created as a prerequisite). You can open it directly from MS Word or by right-click on file and choosing Edit option.
- Paste code snippet into MS Word html document.
- Press Ctrl+H to open search/replace popup.
- Type ^l (caret or shift+6 followed by letter l) in Find what field.
- Type ^p (caret or shift+6 followed by letter p) in Replace with field.
- Press Alt+A to Replace All
- Press Esc to close search/replace popup
- Copy code snippet from MS Word html document.
- Paste into SCN Editor.
- Close MS Word html document without saving so that same steps can be followed next time.
Here is a screenshot that shows search/replace operation.
Final Result
After following steps, syntax highlighted code with indentation can be pasted in SCN Editor as shown below.
* get workitem runtime object
CALL METHOD cl_swf_run_wim_factory=>find_by_wiid
EXPORTING
im_wiid = workitem_id
RECEIVING
re_instance = lv_wi_handle.
//
Once again new learning from your blog.. Thanks!
REPORT zr_test_scn.
CLASS lcl_local DEFINITION.
PUBLIC SECTION.
CLASS-METHODS start.
ENDCLASS. "lcl_local DEFINITION
CLASS lcl_local IMPLEMENTATION.
METHOD start.
MESSAGE s398(00) WITH 'This works, you rock Manish!'.
ENDMETHOD. "start
ENDCLASS. "lcl_local IMPLEMENTATION
START-OF-SELECTION.
lcl_local=>start( ).
It only looses my (indent) spaces for some unknown reason. But it's much better!
Indent is lost because of how MS Word sends data to clipboard.
It strips leading blanks from text that was pasted, but it keeps the blanks if they were added manually inside MS Word.
I'll update the blog after finding an easy way to fix it.
and for some strange reason, it only worked once...
not sure what's going wrong, deleted the files, recreated it, but no syntax highlighting anymore...
Please check whether the file was saved as Web page, Filtered (*.htm) format and closed on first run.
The actual step is to open that htm file in MS Word, and then paste code.
And now it works again. Still not sure what I did wrong last time.
Is it SCN's intention to create a 9th generation copy and pasting programming language or why is there not an ABAP editor <quote> </quote>?
Faffing around with this html markup is a pain for a community site where BPX and SPRO folks are also meant to be able to feel at home...
I also noticed that when hitting Backspace or even Enter sometimes, then I loose blocks of text. Am on Windows 8.1, IE 11, 64 bit. Anyone else noticed that?
Or is Windows 8.1 not supported?
Cheers,
Julius
Unless these issues make it to known issues list and a Jive ticket is raised, I don't think Jive will know about them.
I also have one example of disappearing block of text.
After copying a block of text from Notepad that has 1 non-blank line, few blank lines, and 1 non-blank line, pasting it in SCN Editor will loose the blank lines.
But, as soon as you select all text (Ctrl+A), the blank lines appear.
As a reverse case, copy almost any multi-line text from SCN post or editor, and paste in Notepad, every newline gets converted to double newlines. This is the reason why so many code snippets take up twice the vertical space. As a workaround, The multi-line text needs to be pasted into MS Word first, and then copying from MS Word to Notepad will not double the blank lines.
Because of such text issues, users would end up pasting screenshots of text, which will end up taking more server space, and hence less green method.
Yes, I think that is what is happening to me - thanks for helping to work it out!
I will try the MS Word approach as "next generation front end integration technology".
Cheers,
Julius
ps: Reminds me a bit of the joke about some banana republics in Africa: "What was used in Zimbabwe before there were candles?"
Hi all,
when I read this blog I started looking on the web for better ways for syntax highlighting of ABAP code. I found the following two web sites that do a pretty good job in producing html markup that can be posted into any blog:
http://hilite.me/
http://tohtml.com/abap4/
Best,
Christian
Hi Christian
The highlighting of hilite.me is better than tohtml.com as far as ABAP code is concerned.
Although it is not exactly same as New ABAP Editor, it is better than many other websites, and it can be posted to SCN Editor.
Thanks.
Hello Manish,
So do you say we can use these 2 websites instead of your method?
- Suhas
Hi Suhas
Both methods have their own advantages, and you can pick any.
My method is able to post highlighted code exactly like New ABAP Editor.
It is dependent on MS Office, which is good for average person in corporate environment, and bad for open source lovers. Some corporate environments provide very restricted internet, and the website may not be accessible for all users. My corporate environment is one of them 🙁
The hilite.me website is good enough, suited to a person can't/won't MS Office.
Although i have written this blog, i personally don't use the MS Office method.
While posting snippets, i use a very ugly method which is able to produce this.
//
Hello Manish,
I have MS Office 2010 suite at work and i tried your procedure but unfortunately it doesn't work 🙁
Can you disclose your dirty trick so that i can give it a try?
BR,
Suhas
It should work if you open a Web page, filtered format html file from MS Word and then copy/paste from SAPGUI>>Word and then Word>>SCN.
About the other trick, i'll post it as another personal blog.
EDIT: My work machine also has Word 2010 after upgrade. I can confirm that this blog is not applicable for Word 2010. Reason being, the Web page, Filtered *.htm file created by Word 2007 has metadata Microsoft Word 12 (filtered), whereas Word 2010 's html has metadata Microsoft Word 14 (filtered).
So the html generator engine of Word has undergone some changes.
Hello Manish,
This seems to be a bit complex when compared to the other code snippets.
Can you please suggest any other snippets which you have used.
Regards,
Thanga