ABAP code lighter for SCN.sap.com

+ -

Hello colleagues!

As there is no support of ABAP programming language in syntax highlighting in SCN's WYSIWYG editor at the moment, I wrote the ABAP code lighter for scn.sap.com.

1.png

 

How to install

In order to install the ABAP code lighter, just copy the source code below and paste it into your ABAP editor (SE80). Then activate it (Ctrl+F3).

 

How to use

1. Copy the source code of your ABAP program in the text editor at the left, then press F8.

1.png

2. Copy generated HTML code from the text editor at the right and paste it in your SCN post in HTML mode.

2.png

Below you can see the source code of the progrm and the result of its work at the same time.

I hope you will like it!

*&---------------------------------------------------------------------*

*& Report  Z_CODE_LIGHTER

*&

*&---------------------------------------------------------------------*

*& by George Shlyahov @ SCN.sap.com

*& http_://scn.sap.com/people/george.shlyahov

*&---------------------------------------------------------------------*

 

REPORT z_code_lighter.

 

CONSTANTS:

  gc_begin         TYPE text1024 VALUE `<BODY_BEGIN>`,

  gc_end           TYPE text1024 VALUE `</BODY_END>`,

  gc_blue          TYPE text1024 VALUE `<SPAN_BLUE>`,

  gc_grey          TYPE text1024 VALUE `<SPAN_GREY>`,

  gc_green         TYPE text1024 VALUE `<SPAN_GREEN>`,

  gc_light_blue    TYPE text1024 VALUE `<SPAN_LIGHT_BLUE>`,

  gc_brown         TYPE text1024 VALUE `<SPAN_BROWN>`,

  gc_violet        TYPE text1024 VALUE `<SPAN_VIOLET>`,

  gc_span_close    TYPE text1024 VALUE `</SPAN_CLOSE>`,

  gc_italic        TYPE text1024 VALUE `<ITALIC>`,

  gc_italic_close  TYPE text1024 VALUE `</ITALIC_CLOSE>`,

  gc_dummy         TYPE text1024 VALUE `<DUMMY>`,

 

  gc_begin0        TYPE text1024 VALUE `<PRE class='jive_text_macro jive_macro_quote' jivemacro='quote' _modifiedtitle='true'>`,

  gc_end0          TYPE text1024 VALUE `</PRE>`,

  gc_blue0         TYPE text1024 VALUE `<SPAN style='color: #0000ff;'>`,

  gc_grey0         TYPE text1024 VALUE `<SPAN style='color: #808080;'>`,

  gc_green0        TYPE text1024 VALUE `<SPAN style='color: #4DA616;'>`,

  gc_light_blue0   TYPE text1024 VALUE `<SPAN style='color: #3399FF;'>`,

  gc_brown0        TYPE text1024 VALUE `<SPAN style='color: #800097;'>`,

  gc_violet0       TYPE text1024 VALUE `<SPAN style='color: #800080;'>`,

  gc_span_close0   TYPE text1024 VALUE `</SPAN>`,

  gc_italic0       TYPE text1024 VALUE `<EM>`,

  gc_italic_close0 TYPE text1024 VALUE `</EM>`.

 

TYPE-POOLS abap.

 

TYPES:

  BEGIN OF ty_keyword,

    word TYPE text50,

  END OF ty_keyword.

 

DATA:

  go_dock TYPE REF TO cl_gui_docking_container,

  go_container_1 TYPE REF TO cl_gui_container,

  go_editor_1    TYPE REF TO cl_gui_textedit,

  go_container_2 TYPE REF TO cl_gui_container,

  go_editor_2    TYPE REF TO cl_gui_textedit,

  gt_code        TYPE STANDARD TABLE OF text1024,

  gt_keywords    TYPE STANDARD TABLE OF ty_keyword,

  g_suppress     TYPE abap_bool.

 

 

REFRESH gt_keywords.

PERFORM words.

 

SELECTION-SCREEN BEGIN OF SCREEN 2000.

SELECTION-SCREEN END OF SCREEN 2000.

 

START-OF-SELECTION.

  CALL SELECTION-SCREEN 2000.

 

AT SELECTION-SCREEN.

  REFRESH gt_code.

  CALL METHOD go_editor_1->get_text_as_r3table

    IMPORTING

      table = gt_code.

 

  PERFORM color.

 

  FIELD-SYMBOLS <s_code> LIKE LINE OF gt_code.

  LOOP AT gt_code ASSIGNING <s_code>.

    CONCATENATE `<P style='font-family:courier;color:black'>` <s_code> '</P>' INTO <s_code>.

  ENDLOOP.

 

  DATA: gt_code_tmp LIKE gt_code.

  gt_code_tmp = gt_code.

  REFRESH gt_code.

 

  APPEND gc_begin TO gt_code.

  APPEND LINES OF gt_code_tmp TO gt_code.

  APPEND gc_end TO gt_code.

 

  LOOP AT gt_code ASSIGNING <s_code>.

    REPLACE ALL OCCURRENCES OF gc_begin        IN <s_code> WITH gc_begin0.

    REPLACE ALL OCCURRENCES OF gc_end          IN <s_code> WITH gc_end0.

    REPLACE ALL OCCURRENCES OF gc_blue         IN <s_code> WITH gc_blue0.

    REPLACE ALL OCCURRENCES OF gc_grey         IN <s_code> WITH gc_grey0.

    REPLACE ALL OCCURRENCES OF gc_green        IN <s_code> WITH gc_green0.

    REPLACE ALL OCCURRENCES OF gc_light_blue   IN <s_code> WITH gc_light_blue0.

    REPLACE ALL OCCURRENCES OF gc_brown        IN <s_code> WITH gc_brown0.

    REPLACE ALL OCCURRENCES OF gc_violet       IN <s_code> WITH gc_violet0.

    REPLACE ALL OCCURRENCES OF gc_span_close   IN <s_code> WITH gc_span_close0.

    REPLACE ALL OCCURRENCES OF gc_italic       IN <s_code> WITH gc_italic0.

    REPLACE ALL OCCURRENCES OF gc_italic_close IN <s_code> WITH gc_italic_close0.

  ENDLOOP.

 

  CALL METHOD go_editor_2->set_text_as_r3table

    EXPORTING

      table          = gt_code

    EXCEPTIONS

      error_dp       = 1

      error_dp_create = 2

      OTHERS         = 3.

 

  CALL SELECTION-SCREEN 2000.

 

AT SELECTION-SCREEN OUTPUT.

  CHECK go_dock IS NOT BOUND.

  CREATE OBJECT go_dock

    EXPORTING

      parent = cl_gui_container=>screen0

      side  = cl_gui_docking_container=>dock_at_left

      ratio = 90.

 

  DATA go_splitter TYPE REF TO cl_gui_splitter_container.

 

  CREATE OBJECT go_splitter

    EXPORTING

      parent = go_dock

      rows   = 1

      columns = 2.

 

  CHECK go_container_1 IS NOT BOUND.

  CALL METHOD go_splitter->get_container

    EXPORTING

      row      = 1

      column   = 1

    RECEIVING

      container = go_container_1.

 

  CHECK go_editor_1 IS NOT BOUND.

 

  CREATE OBJECT go_editor_1

    EXPORTING

      parent                    = go_container_1

      wordwrap_mode              = cl_gui_textedit=>wordwrap_at_fixed_position

      wordwrap_position          = 1024

      wordwrap_to_linebreak_mode = cl_gui_textedit=>true.

 

  CHECK go_container_2 IS NOT BOUND.

  CALL METHOD go_splitter->get_container

    EXPORTING

      row      = 1

      column   = 2

    RECEIVING

      container = go_container_2.

 

  CHECK go_container_2 IS BOUND.

  CHECK go_editor_2 IS NOT BOUND.

 

  CREATE OBJECT go_editor_2

    EXPORTING

      parent                    = go_container_2

      wordwrap_mode             = cl_gui_textedit=>wordwrap_at_fixed_position

      wordwrap_position         = 1024

      wordwrap_to_linebreak_mode = cl_gui_textedit=>true.

 

  IF go_editor_2 IS BOUND.

    CALL METHOD go_editor_2->set_readonly_mode.

  ENDIF.

 

 

*&---------------------------------------------------------------------*

*&      Form  color

*&---------------------------------------------------------------------*

*       text

*----------------------------------------------------------------------*

FORM color.

 

  DATA:

    l_gt  TYPE text100,

    l_lt  TYPE text100,

    l_gt2 TYPE text100,

    l_lt2 TYPE text100.

 

  FIELD-SYMBOLS <s_code> LIKE LINE OF gt_code.

 

 

  LOOP AT gt_code ASSIGNING <s_code>.

 

    CONCATENATE '&g' 't;' INTO l_gt.

    CONCATENATE '&l' 't;' INTO l_lt.

 

    REPLACE ALL OCCURRENCES OF '>' IN <s_code> WITH l_gt.

    REPLACE ALL OCCURRENCES OF '<' IN <s_code> WITH l_lt.

 

    CONCATENATE gc_brown l_gt gc_span_close INTO l_gt2.

    CONCATENATE gc_brown l_lt gc_span_close INTO l_lt2.

 

    REPLACE ALL OCCURRENCES OF l_gt IN <s_code> WITH l_gt2.

    REPLACE ALL OCCURRENCES OF l_lt IN <s_code> WITH l_lt2.

 

    IF <s_code>(1) = '*'.

*     Comments with asterisk:

      PERFORM color_comments USING '\*.*' gc_grey gc_span_close

                          CHANGING <s_code>.

    ELSE.

*     Key words:

      PERFORM color_key_words USING '[a-zA-Z_-]+\w' gc_blue gc_span_close

                           CHANGING <s_code>.

 

*     Digits in brackets:

      DATA: l_open  TYPE text1024,

            l_close TYPE text1024.

      CONCATENATE '(' gc_light_blue INTO l_open.

      CONCATENATE gc_span_close ')' INTO l_close.

      PERFORM color_text USING '\((\d+)\)' "regex

                               l_open      "open tag

                               l_close     "close tag

                      CHANGING <s_code>.

 

      CONCATENATE gc_brown '(' gc_span_close INTO l_gt.

      CONCATENATE gc_brown ')' gc_span_close INTO l_lt.

      REPLACE ALL OCCURRENCES OF '(' IN <s_code> WITH l_gt.

      REPLACE ALL OCCURRENCES OF ')' IN <s_code> WITH l_lt.

 

*     Detached digits (TODO):

      PERFORM color_text USING '\d\d*' "regex

                                gc_light_blue           "open tag

                                gc_span_close           "close tag

                       CHANGING <s_code>.

 

      CONCATENATE gc_violet ',' gc_span_close INTO l_gt.

      CONCATENATE gc_violet '.' gc_span_close INTO l_lt.

      REPLACE ALL OCCURRENCES OF ',' IN <s_code> WITH l_gt.

      REPLACE ALL OCCURRENCES OF '.' IN <s_code> WITH l_lt.

 

      CONCATENATE gc_violet `=>` gc_span_close INTO l_gt.

      REPLACE ALL OCCURRENCES OF `=>` IN <s_code> WITH l_gt.

 

      CONCATENATE gc_violet `->` gc_span_close INTO l_gt.

      REPLACE ALL OCCURRENCES OF `->` IN <s_code> WITH l_gt.

 

      CONCATENATE gc_violet `&nb` `sp;` `=` `&nb` `sp;` gc_span_close INTO l_gt.

      REPLACE ALL OCCURRENCES OF ` = ` IN <s_code> WITH l_gt.

 

*     Comments with quotation marks:

      PERFORM color_comments USING '".*' gc_grey gc_span_close

                          CHANGING <s_code>.

 

*     Text in inverted commas:

      PERFORM color_text USING `([\'][^\']*[\'])` gc_green gc_span_close

                      CHANGING <s_code>.

 

*     Texs in SQL-like inverted commas:

      PERFORM color_text USING '([\`][^\`]*[\`])' gc_green gc_span_close

                      CHANGING <s_code>.

 

    ENDIF.

  ENDLOOP.

 

ENDFORM.                    "color

 

 

*&---------------------------------------------------------------------*

*&      Form  color_key_words

*&---------------------------------------------------------------------*

*       text

*----------------------------------------------------------------------*

*      -->U_REGEX      text

*      -->U_OPEN_TAG   text

*      -->U_CLOSE_TAG  text

*      -->CS_CODE      text

*----------------------------------------------------------------------*

FORM color_key_words USING u_regex     TYPE text1024

                           u_open_tag  TYPE text1024

                           u_close_tag TYPE text1024

                  CHANGING cs_code     TYPE any.

 

  DATA:

    lt_code    LIKE gt_code,

    lo_matcher TYPE REF TO cl_abap_matcher,

    lf_success TYPE abap_bool VALUE abap_true,

    ls_match   TYPE match_result,

    l_keyword  TYPE text1024,

    l_offset   TYPE i,

    l_newtext  TYPE text1024.

 

  FIELD-SYMBOLS:

    <s_line>  LIKE LINE OF lo_matcher->table,

    <s_code>  LIKE LINE OF gt_code,