CLASS zcl_abapgit_html DEFINITION
PUBLIC
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES zif_abapgit_html .
CONSTANTS c_indent_size TYPE i VALUE 2 ##NO_TEXT.
CLASS-METHODS class_constructor .
CLASS-METHODS create
IMPORTING
!iv_initial_chunk TYPE any OPTIONAL
RETURNING
VALUE(ri_instance) TYPE REF TO zif_abapgit_html.
CLASS-METHODS icon
IMPORTING
!iv_name TYPE string
!iv_hint TYPE string OPTIONAL
!iv_class TYPE string OPTIONAL
!iv_onclick TYPE string OPTIONAL
RETURNING
VALUE(rv_str) TYPE string .
CLASS-METHODS checkbox
IMPORTING
iv_id TYPE string OPTIONAL
iv_checked TYPE abap_bool OPTIONAL
RETURNING
VALUE(rv_html) TYPE string .
CLASS-METHODS parse_data_attr
IMPORTING
iv_str TYPE string OPTIONAL
RETURNING
VALUE(rs_data_attr) TYPE zif_abapgit_html=>ty_data_attr .
CLASS-METHODS set_debug_mode
IMPORTING
iv_mode TYPE abap_bool.
PROTECTED SECTION.
PRIVATE SECTION.
CONSTANTS c_max_indent TYPE i VALUE 200.
TYPES:
BEGIN OF ty_indent_context,
no_indent_jscss TYPE abap_bool,
within_style TYPE abap_bool,
within_js TYPE abap_bool,
within_textarea TYPE abap_bool,
within_pre TYPE abap_bool,
indent TYPE i,
indent_str TYPE string,
END OF ty_indent_context .
TYPES:
BEGIN OF ty_study_result,
style_open TYPE abap_bool,
style_close TYPE abap_bool,
script_open TYPE abap_bool,
script_close TYPE abap_bool,
textarea_open TYPE abap_bool,
textarea_close TYPE abap_bool,
pre_open TYPE abap_bool,
pre_close TYPE abap_bool,
tag_close TYPE abap_bool,
curly_close TYPE abap_bool,
openings TYPE i,
closings TYPE i,
singles TYPE i,
END OF ty_study_result .
CLASS-DATA go_single_tags_re TYPE REF TO cl_abap_regex .
DATA mt_buffer TYPE string_table .
CLASS-DATA gv_spaces TYPE string .
CLASS-DATA gv_debug_mode TYPE abap_bool .
METHODS indent_line
CHANGING
!cs_context TYPE ty_indent_context
!cv_line TYPE string .
METHODS study_line
IMPORTING
!iv_line TYPE string
!is_context TYPE ty_indent_context
RETURNING
VALUE(rs_result) TYPE ty_study_result .
ENDCLASS.
CLASS zcl_abapgit_html IMPLEMENTATION.
METHOD checkbox.
DATA: lv_checked TYPE string.
IF iv_checked = abap_true.
lv_checked = |checked|.
ENDIF.
rv_html = |`.
ENDMETHOD.
METHOD class_constructor.
CREATE OBJECT go_single_tags_re
EXPORTING
pattern = '<(AREA|BASE|BR|COL|COMMAND|EMBED|HR|IMG|INPUT|LINK|META|PARAM|SOURCE|!)'
ignore_case = abap_false.
gv_spaces = repeat(
val = ` `
occ = c_max_indent ).
ENDMETHOD.
METHOD create.
CREATE OBJECT ri_instance TYPE zcl_abapgit_html.
IF iv_initial_chunk IS NOT INITIAL.
ri_instance->add( iv_initial_chunk ).
ENDIF.
ENDMETHOD.
METHOD icon.
DATA: lv_hint TYPE string,
lv_name TYPE string,
lv_color TYPE string,
lv_class TYPE string,
lv_onclick TYPE string.
SPLIT iv_name AT '/' INTO lv_name lv_color.
IF iv_hint IS NOT INITIAL.
lv_hint = | title="{ iv_hint }"|.
ENDIF.
IF iv_onclick IS NOT INITIAL.
lv_onclick = | onclick="{ iv_onclick }"|.
ENDIF.
IF iv_class IS NOT INITIAL.
lv_class = | { iv_class }|.
ENDIF.
IF lv_color IS NOT INITIAL.
lv_color = | { lv_color }|.
ENDIF.
rv_str = ||.
ENDMETHOD.
METHOD indent_line.
DATA: ls_study TYPE ty_study_result,
lv_spaces TYPE i.
ls_study = study_line(
is_context = cs_context
iv_line = cv_line ).
" No indent for textarea tags
IF ls_study-textarea_open = abap_true.
cs_context-within_textarea = abap_true.
RETURN.
ELSEIF ls_study-textarea_close = abap_true.
cs_context-within_textarea = abap_false.
RETURN.
ELSEIF cs_context-within_textarea = abap_true.
RETURN.
ENDIF.
" No indent for pre tags
IF ls_study-pre_open = abap_true.
cs_context-within_pre = abap_true.
RETURN.
ELSEIF ls_study-pre_close = abap_true.
cs_context-within_pre = abap_false.
RETURN.
ELSEIF cs_context-within_pre = abap_true.
RETURN.
ENDIF.
" First closing tag - shift back exceptionally
IF ( ls_study-script_close = abap_true
OR ls_study-style_close = abap_true
OR ls_study-curly_close = abap_true
OR ls_study-tag_close = abap_true )
AND cs_context-indent > 0.
lv_spaces = ( cs_context-indent - 1 ) * c_indent_size.
IF lv_spaces <= c_max_indent.
cv_line = gv_spaces(lv_spaces) && cv_line.
ELSE.
cv_line = gv_spaces && cv_line.
ENDIF.
ELSE.
cv_line = cs_context-indent_str && cv_line.
ENDIF.
" Context status update
CASE abap_true.
WHEN ls_study-script_open.
cs_context-within_js = abap_true.
cs_context-within_style = abap_false.
WHEN ls_study-style_open.
cs_context-within_js = abap_false.
cs_context-within_style = abap_true.
WHEN ls_study-script_close OR ls_study-style_close.
cs_context-within_js = abap_false.
cs_context-within_style = abap_false.
ls_study-closings = ls_study-closings + 1.
ENDCASE.
" More-less logic chosen due to possible double tags in a line ''
IF ls_study-openings <> ls_study-closings.
IF ls_study-openings > ls_study-closings.
cs_context-indent = cs_context-indent + 1.
ELSEIF cs_context-indent > 0. " AND ls_study-openings < ls_study-closings
cs_context-indent = cs_context-indent - 1.
ENDIF.
lv_spaces = cs_context-indent * c_indent_size.
IF lv_spaces <= c_max_indent.
cs_context-indent_str = gv_spaces(lv_spaces).
ELSE.
cv_line = gv_spaces && cv_line.
ENDIF.
ENDIF.
ENDMETHOD.
METHOD parse_data_attr.
SPLIT iv_str AT '=' INTO rs_data_attr-name rs_data_attr-value.
IF rs_data_attr-name IS INITIAL.
CLEAR rs_data_attr.
ENDIF.
ENDMETHOD.
METHOD set_debug_mode.
gv_debug_mode = iv_mode.
ENDMETHOD.
METHOD study_line.
DATA: lv_line TYPE string,
lv_len TYPE i.
lv_line = to_upper( shift_left( val = iv_line
sub = ` ` ) ).
lv_len = strlen( lv_line ).
" Some assumptions for simplification and speed
" - style & scripts tag should be opened/closed in a separate line
" - style & scripts opening and closing in one line is possible but only once
" TODO & Issues
" - What if the string IS a well formed html already not just single line ?
IF is_context-within_js = abap_true OR is_context-within_style = abap_true.
IF is_context-within_js = abap_true AND lv_len >= 8 AND lv_line(8) = '= 7 AND lv_line(7) = '= 1 AND lv_line(1) = '}'.
rs_result-curly_close = abap_true.
ENDIF.
FIND ALL OCCURRENCES OF '{' IN lv_line MATCH COUNT rs_result-openings.
FIND ALL OCCURRENCES OF '}' IN lv_line MATCH COUNT rs_result-closings.
ENDIF.
ELSE.
IF lv_len >= 7 AND lv_line(7) = '