As mentioned in my blog A Github repository issue backup tool developed by ABAP I am developing a tool in ABAP which can backup my github issues.
One of the functionality I would like to fulfill is to backup the images used in issue with markdown format as well. Take this issue of mine for example. This issue contains the reference to image files uploaded by me via dragging my local image files and drop them to the browser.
One of the functionality I would like to fulfill is to backup the images used in issue with markdown format as well. Take this issue of mine for example. This issue contains the reference to image files uploaded by me via dragging my local image files and drop them to the browser.
The syntax is , here below is an example:

Since I have already downloaded the source code of all github issues belonging to a given github repository into ABAP system, the next task for me is to develop a function module which accepts github issue source code with markdown format as input, and generate an internal table as output which looks like below:
Use JavaScript regular expression to parse image reference in markdown source code
I have written the following code to parse the image reference in one of my github issue:
<html>
<script>
const regex = /!\[(.*?)\]\((.*?)\)/g;
const str = `# Created by Jerry Wang, last modified on Jun 24, 2014
User parameter里只要BSPWD_USER_LEVEL 值大于5 即可在webclient UI上显示error message的technical information:




这里决定用什么icon来在UI上显示message:

message level > 5的判断:


`;
let m;
var printResult = ( array) => {
var url = array[2];
var splited = url.split(".");
console.log("local file: " + array[1] + "." + splited[splited.length-1]);
console.log("url: " + url);
};
while ((m = regex.exec(str)) !== null) {
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
printResult(m);
}
</script>
</html>
The output shows this regular expression works as expected:
Solution in ABAP
When I copy the working regular expression !\[(.*?)\]\((.*?)\) in JavaScript to ABAP, it does not work – this regular expression is complained as invalid.
From this stackoverflow discussion thread Greedy/non-greedy quantifiers in ABAP regular expressions I get to know that the symbol ? for non-greedy match behavior in regular expression is not supported in ABAP.
Thus I have to use some alternative to achieve the same output as in JavaScript.
Approach1 – use dynamically generated regular expression
My idea is to first identify all occurrence of “” by dynamically generation of corresponding regular expression pattern.
I have written another tool class to achieve this requirement:
class CL_ABAP_GIT_ISSUE_IMAGE_TOOL definition
public
final
create public .
public section.
types:
BEGIN OF ty_image_reference,
image_name TYPE string,
image_url TYPE string,
END OF ty_image_reference .
types:
tt_image_reference TYPE TABLE OF ty_image_reference with key image_name .
class-methods GET_IMAGE_REFERENCE
importing
!IV_ISSUE_SOURCE_CODE type STRING
returning
value(RT_IMAGE) type TT_IMAGE_REFERENCE .
protected section.
private section.
class-data SV_IMAGE_PATTERN type STRING value '(!\[.*\]\(.*\))' ##NO_TEXT.
ENDCLASS.
CLASS CL_ABAP_GIT_ISSUE_IMAGE_TOOL IMPLEMENTATION.
METHOD get_image_reference.
DATA: lv_reg_pattern TYPE string,
lt_result_tab TYPE match_result_tab.
FIND ALL OCCURRENCES OF '









No comments:
Post a Comment