Possible values for text run arrays
The article lays out the structure and possible values for a text run array. Use this article as a reference when writing PHP code that iterates over a text run array.
A text run array contains any number of paragraphs objects which are referred to by PARAGRAPH_KEY below.
[PARAGRAPH_KEY]->metadata->style: empty or 'code'.
[PARAGRAPH_KEY]->style->align: empty, 'left, 'right', 'center'. Always empty right now.
[PARAGRAPH_KEY]->style->list_style: = empty, 'disc' or 'decimal'.
[PARAGRAPH_KEY]->style->list_depth: = Integer specifying the indentation for the list.
[PARAGRAPH_KEY]->style->list_index: = Integer specifying the starting number for the list.
[PARAGRAPH_KEY]->style->left_indent: = A positive value if the text is indented.
[PARAGRAPH_KEY]->runs: An array of run objects. Referred to as [RUN_KEY] below.
[RUN_KEY]->style->font_family: The font to use for the run of text.
[RUN_KEY]->style->font_size: The size of the font.
[RUN_KEY]->style->font_styles: empty or any combination of 'italic', 'bold', and 'underline' (e.g. 'bold,italic');
[RUN_KEY]->style->color: The color of the text in RGB format.
[RUN_KEY]->style->text_shift: empty or an integer <> 0. If the integer is positive then the text is subscript. If the integer is negative then the text is superscript.
[RUN_KEY]->style->link: The hyperlink assigned to the text.
[RUN_KEY]->metadata->style: empty or 'code'
[RUN_KEY]->text: The text.
Example PHP code for iterating over a text run array
Here is some sample code that iterates over a text run array and generates markup appropriate for MediaWiki.
function printTextRunAsMediaWiki($textrun, $type='instructions') {
$output = '';
$listDepth = 0;
// If there is no text then just return an empty string.
if (!is_array($textrun)) return '';
// Iterate through paragraphs.
foreach($textrun as $para)
{
$closingPara = '';
/* Unused
$para->style->align
*/
// Is this paragraph formatted as code?
// If not is it a list item?
if ($para->metadata->style == 'code')
{
$output .= '<code>';
$closingPara = '</code>';
} else {
switch ($para->style->list_style)
{
case 'decimal':
$output .= str_repeat('#', $para->style->list_depth) . ' ';
break;
default:
$output .= str_repeat('*', $para->style->list_depth) . ' ';
break;
}
}
// Iterate through each text run in the paragraph.
if (isset($para->runs))
{
foreach ($para->runs as $run)
{
$closingRun = '';
$prefix = '';
$suffix = '';
$styles = explode(',', $run->style->font_styles);
$hasBold = array_search('bold', $styles) != FALSE;
$hasItalic = array_search('italic', $styles) != FALSE;
$hasUnderline = array_search('underline', $styles) != FALSE;
if (!empty($run->style->color))
{
$output .= '<span style="color: rgb(' . $run->style->color . ');">';
$closingRun = '</span>';
}
if ($hasItalic) { $prefix .= "''"; $suffix = "''" . $suffix; }
if ($hasBold) { $prefix .= "'''"; $suffix = "'''" . $suffix; }
if ($hasUnderline) { $prefix .= '<ul>'; $suffix = '</ul>' . $suffix; }
/* Unused
$run->style->font_family
$run->style->font_size
$run->style->text_shift
*/
$output .= $prefix;
$output .= $run->text;
$output .= $suffix;
$output .= $closingRun;
}
}
$output .= $closingPara;
if ($type != 'title') $output .= PHP_EOL;
}
return $output;
}
0 Comments
Add your comment