Erroneous translation of Trac-style links to Markdown inside code blocks
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;
}
?>
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 could you make a pull request with the fix please?
Only this special one on the links or all of the general Markdown issues?
@tbeu if you have other fixes it would be cool to make them mainstream for others too 👍
Yes, I will provide PR soon, but also need to do some testing before.
@tbeu sounds good, looking forward to it.
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 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
Yes, saw this already. Since I am on 5.4.16 I did not care for 5.3 fallback.