trustmaster / trac2github

Converts Trac milestones, tickets and comments into Github issues 2.0 using github api v3
49 stars 38 forks source link

Erroneous translation of Trac-style links to Markdown inside code blocks #29

Open tbeu opened 8 years ago

tbeu commented 8 years ago

The following code in translate_markup

    // Translate Trac-style links to Markdown
    $data = preg_replace('/\[([^ ]+) ([^\]]+)\]/', '[$2]($1)', $data);

should not be applied inside a code block.

Example file: mo.txt Example code for easy reproduction:

<?php

$data = file_get_contents("mo.txt");

file_put_contents("gh.txt", translate_markup($data));

function translate_markup($data) {
    // Replace code blocks with an associated language
    $data = preg_replace('/\{\{\{(\s*#!(\w+))?/m', '```$2', $data);
    $data = preg_replace('/\}\}\}/', '```', $data);

    // Avoid non-ASCII characters, as that will cause trouble with json_encode()
    $data = preg_replace('/[^(\x00-\x7F)]*/','', $data);

    // Translate Trac-style links to Markdown
    $data = preg_replace('/\[([^ ]+) ([^\]]+)\]/', '[$2]($1)', $data);

    // Possibly translate other markup as well?
    return $data;
}

?>

mo.txt

tbeu commented 7 years ago

See the updated function translate_markup in https://github.com/modelica-trac-importer/trac2github/blob/import-MSL/trac2github.php that fixes this and many other Markdown related issues.

trustmaster commented 7 years ago

@tbeu could you make a pull request with the fix please?

tbeu commented 7 years ago

Only this special one on the links or all of the general Markdown issues?

trustmaster commented 7 years ago

@tbeu if you have other fixes it would be cool to make them mainstream for others too 👍

tbeu commented 7 years ago

Yes, I will provide PR soon, but also need to do some testing before.

trustmaster commented 7 years ago

@tbeu sounds good, looking forward to it.

tbeu commented 7 years ago

It is not forgotten. The encoding issue can be solved with json_encode($data, JSON_UNESCAPED_UNICODE) of PHP >= 5.4. Would this be an accepted PHP version constraint?

trustmaster commented 7 years ago

@tbeu i guess so, but for safety should use version_compare(PHP_VERSION, '5.4.0', '>=') to check first. And/or use this hint: http://php.net/manual/en/function.json-encode.php#105789

tbeu commented 7 years ago

Yes, saw this already. Since I am on 5.4.16 I did not care for 5.3 fallback.