trac2github icon indicating copy to clipboard operation
trac2github copied to clipboard

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

Open tbeu opened this issue 9 years ago • 9 comments

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 avatar Oct 24 '16 08:10 tbeu

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.

tbeu avatar Jan 02 '17 09:01 tbeu

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

trustmaster avatar Jan 02 '17 11:01 trustmaster

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

tbeu avatar Jan 02 '17 11:01 tbeu

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

trustmaster avatar Jan 02 '17 12:01 trustmaster

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

tbeu avatar Jan 02 '17 12:01 tbeu

@tbeu sounds good, looking forward to it.

trustmaster avatar Jan 02 '17 12:01 trustmaster

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?

tbeu avatar Jan 13 '17 20:01 tbeu

@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

trustmaster avatar Jan 13 '17 20:01 trustmaster

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

tbeu avatar Jan 13 '17 21:01 tbeu