Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1906 from Maks3w/hotfix/docbook-to-rst
Browse files Browse the repository at this point in the history
[Doc2Rst] indentation, titles and escape chars
  • Loading branch information
ezimuel committed Jul 17, 2012
2 parents 9f4dd7f + caa6852 commit 7e444f8
Show file tree
Hide file tree
Showing 2 changed files with 247 additions and 211 deletions.
93 changes: 76 additions & 17 deletions bin/doc2rst.php
Expand Up @@ -140,7 +140,7 @@ class RstConvert
public static $footnote = array();

/**
* Indent a text 4 spaces
* Indent the text 3 spaces by default
*
* @param string $text
* @return string
Expand All @@ -159,6 +159,46 @@ public static function indent($text)
return $output;
}

/**
* Indent the text 2 spaces
*
* @param string $text
* @return string
*/
public static function indent2($text)
{
$rows = explode("\n", $text);
$output = '';
foreach ($rows as $row) {
if ($row === '' || preg_match('/^\s+$/', $row)) {
$output .= "\n";
} else {
$output .= " $row\n";
}
}
return $output;
}

/**
* Indent the text 7 spaces
*
* @param string $text
* @return string
*/
public static function indent7($text)
{
$rows = explode("\n", $text);
$output = '';
foreach ($rows as $row) {
if ($row === '' || preg_match('/^\s+$/', $row)) {
$output .= "\n";
} else {
$output .= " $row\n";
}
}
return $output;
}

/**
* Convert all the section/title, except for the first
*
Expand All @@ -175,7 +215,7 @@ public static function title($text, $sign = '-', $top = false)
if ($top) {
$output = $line . "\n" . $output;
}
return "\n" . $output . "\n";
return $output . "\n";
}

/**
Expand All @@ -188,27 +228,35 @@ public static function title($text, $sign = '-', $top = false)
*/
public static function formatText($text, $preceding = false, $following = false)
{
$text = self::escapeChars(trim(preg_replace('/\s+/m', ' ', $text)));
if (!empty($preceding)) {
// Escape whitespace for plurals
if (preg_match('/^s\s/', $text)) {
$text = '\ ' . $text;
} elseif (!in_array($text[0],
array('-', '.', ',', ':', ';', '!', '?', '\\', '/', "'", '"', ')', ']', '}', '>', ' '))
$hasPreceding = !empty($preceding);
$hasFollowing = !empty($following);
$escaped = self::escapeChars(trim(preg_replace('/\s+/m', ' ', $text)));

if ($hasPreceding) {
if (!in_array($escaped[0],
array('-', '.', ',', ':', ';', '!', '?', '\\', '/', "'", '"', ')', ']', '}', '>', ' '))
) {
$text = ' ' . $text;
$escaped = ' ' . $escaped;
if (preg_match('/[^\s]/', $text[0])) {
$escaped = '\\' . $escaped;
}
}
} else {
// Escape characters in the bullet list or format character
if (preg_match('/^([-\+•‣⁃]($|\s)|[_`\*\|])/', $escaped)) {
$escaped = '\\' . $escaped;
}
}

if (!empty($following)) {
if ($hasFollowing) {
if ($following[0]->localName == 'superscript') {
$text .= '\ ';
} elseif (!in_array(substr($text, -1), array('-', '/', "'", '"', '(', '[', '{', '<', ' '))) {
$escaped .= '\ ';
} elseif (!in_array(substr($escaped, -1), array('-', '/', "'", '"', '(', '[', '{', '<', ' '))) {
// Omitted ':' in the list
$text .= ' ';
$escaped .= ' ';
}
}
return $text;
return $escaped;
}

/**
Expand All @@ -224,6 +272,18 @@ public static function escapeChars($text)
str_replace('\\', '\\\\', $text));
}

/**
* Escape an specific char
*
* @param string $text
* @param string $char Char to escape
* @return string
*/
public static function escapeChar($text, $char)
{
return preg_replace(sprintf('/([^\s])(\\%s[^-\.,:;!?\/\\\'"\)\]\}>\s])/', $char), '$1\\\$2', $text);
}

/**
* Convert the link tag
*
Expand All @@ -249,8 +309,7 @@ public static function link($node)
*/
public static function footnote($value)
{
$value = self::formatText($value);
self::$footnote[] = ".. [#] $value";
self::$footnote[] = '.. [#] ' . trim($value);
return '[#]_';
}

Expand Down

0 comments on commit 7e444f8

Please sign in to comment.