1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
|
<?php
class ManpagePhDFormat extends PhDFormat {
protected $elementmap = array( /* {{{ */
'acronym' => 'format_suppressed_tags',
'abbrev' => 'format_suppressed_tags',
'caution' => 'format_admonition',
'citerefentry' => 'format_suppressed_tags',
'classname' => 'format_suppressed_tags',
'classsynopsis' => '.PP',
'classsynopsisinfo' => 'format_suppressed_tags',
'code' => 'format_suppressed_tags',
'command' => '\\fI',
'constant' => '\\fB',
'constructorsynopsis' => 'format_methodsynopsis',
'destructorsynopsis' => 'format_methodsynopsis',
'emphasis' => '\\fI',
'envar' => 'format_suppressed_tags',
'errortype' => 'format_suppressed_tags',
'example' => 'format_example',
'fieldsynopsis' => 'format_suppressed_tags',
'filename' => '\\fI',
'formalpara' => 'format_indent',
/*'funcsynopsis' => '.SH SYNOPSIS',
'funcsynopsisinfo' => '',
'funcprototype' => '',*/
'funcdef' => '.B',
'function' => array(
/* DEFAULT */ 'format_suppressed_tags',
'member' => 'format_suppressed_tags',
),
'glossterm' => 'format_suppressed_tags',
'imagedata' => 'format_suppressed_tags',
'imageobject' => 'format_suppressed_tags',
'informalexample' => '.PP',
'initializer' => array(
/* DEFAULT */ false,
'methodparam' => 'format_suppressed_tags',
),
'interfacename' => 'format_suppressed_tags',
'itemizedlist' => 'format_itemizedlist',
'link' => 'format_suppressed_tags',
'listitem' => array(
/* DEFAULT */ false,
'varlistentry' => 'format_suppressed_tags',
'itemizedlist' => ".TP 0.2i\n\\(bu",
'orderedlist' => ".TP 0.2i\n\\(bu",
),
'literal' => '\\fI',
'literallayout' => 'format_verbatim',
'manvolnum' => 'format_manvolnum',
'mediaobject' => '\\fB[NOT DISPLAYABLE MEDIA]',
'member' => 'format_member',
'methodname' => '\\fB',
'methodparam' => 'format_methodparam',
'methodsynopsis' => 'format_methodsynopsis',
'modifier' => 'format_suppressed_tags',
'note' => 'format_admonition',
'ooclass' => 'format_suppressed_tags',
'option' => '\\fI',
'orderedlist' => 'format_itemizedlist',
'para' => array(
/* DEFAULT */ '.PP',
'listitem' => 'format_suppressed_tags',
'entry' => 'format_suppressed_tags',
),
'paramdef' => 'format_suppressed_tags',
'parameter' => array(
/* DEFAULT */ 'format_suppressed_tags',
'methodparam' => 'format_parameter_method',
'code' => '\\fI',
),
'productname' => 'format_suppressed_tags',
'programlisting' => 'format_verbatim',
'property' => 'format_suppressed_tags',
'refentry' => 'format_suppressed_tags',
'refentrytitle' => '\\fB',
'refname' => '.SH NAME',
'refnamediv' => 'format_suppressed_tags',
'refpurpose' => 'format_refpurpose',
'refsect1' => 'format_refsect',
'refsection' => 'format_refsect',
'refsynopsisdiv' => 'format_refsynopsisdiv',
'replaceable' => '\\fI',
'screen' => 'format_verbatim',
'seg' => 'format_seg',
'seglistitem' => 'format_seglistitem',
'segmentedlist' => 'format_segmentedlist',
'segtitle' => 'format_suppressed_tags',
'simpara' => array(
/* DEFAULT */ '.PP',
'listitem' => '',
),
'simplelist' => 'format_simplelist',
'simplesect' => 'format_suppressed_tags',
'subscript' => 'format_suppressed_tags',
'synopsis' => 'format_suppressed_tags',
'systemitem' => '\\fB',
'tag' => 'format_suppressed_tags',
'term' => 'format_term',
'title' => array(
/* DEFAULT */ '.B',
'segmentedlist' => '.B',
'refsect1' => 'format_refsect_title',
'refsection' => 'format_refsect_title',
),
'tip' => 'format_admonition',
'type' => array(
/* DEFAULT */ '\\fR',
'methodparam' => 'format_suppressed_tags'
),
'userinput' => '\\fB',
'variablelist' => 'format_indent',
'varlistentry' => ".TP 0.2i\n\\(bu",
'varname' => 'format_suppressed_tags',
'void' => 'format_void',
'warning' => 'format_admonition',
'xref' => 'format_xref',
// GROFF (tbl) ARRAYS
'informaltable' => '.P',
'table' => '.P',
'tgroup' => 'format_tgroup',
'colspec' => 'format_suppressed_tags',
'thead' => 'format_thead',
'tbody' => 'format_suppressed_tags',
'row' => 'format_row',
'entry' => 'format_entry',
); /* }}} */
protected $textmap = array(
'classname' => array(
/* DEFAULT */ false,
'ooclass' => 'format_ooclass_name_text',
),
'function' => 'format_function_text',
'initializer' => array(
/* DEFAULT */ false,
'methodparam' => 'format_initializer_method_text',
),
'literallayout' => 'format_verbatim_text',
'manvolnum' => 'format_text',
'parameter' => array(
/* DEFAULT */ 'format_parameter_text',
'code' => false,
//'term' => 'format_parameter_term_text',
'methodparam' => 'format_parameter_method_text',
),
'programlisting' => 'format_verbatim_text',
'refname' => 'format_text',
'refpurpose' => 'format_text',
'screen' => 'format_verbatim_text',
'segtitle' => 'format_segtitle_text',
// 'term' => array(
// /* DEFAULT */ false,
// 'varlistentry' => 'format_term_text',
// ),
'title' => array(
/* DEFAULT */ false,
'refsect' => 'format_refsect_text',
'refsect1' => 'format_refsect_text',
),
'tag' => 'format_tag_text',
'type' => array(
/* DEFAULT */ false,
'methodparam' => 'format_type_method_text',
),
'varname' => 'format_parameter_text',
);
/* Current Chunk variables */
protected $cchunk = array();
/* Default Chunk variables */
protected $dchunk = array(
"appendlater" => false,
"firstitem" => false,
"buffer" => array(),
"examplenumber" => 0,
"methodsynopsis" => array(
"params" => array(),
"firstsynopsis" => true,
),
"open" => false,
"ooclass" => null,
"role" => null,
"segtitle" => array(),
"segindex" => 0,
);
/* If a chunk is being processed */
protected $chunkOpen = false;
/**
* Programlisting role. Necessary to highlight the code properly.
* String when role is set, false if not.
*
* @var string
*
* @see format_programlisting()
* @see CDATA()
*/
public $role = false;
public function __construct(array $IDs) {
parent::__construct($IDs);
}
public function __call($func, $args) {
if ($args[0] && $this->chunkOpen) {
trigger_error("No mapper found for '{$func}'", E_USER_WARNING);
}
return "\n.B [NOT PROCESSED] $args[1] [/NOT PROCESSED]";
}
public function transformFromMap($open, $tag, $name, $attrs, $props) {
if ($tag === '') return $tag;
$isMacro = (strncmp($tag, ".", 1) == 0);
if ($open) {
return "\n" . $tag . ($isMacro ? "\n" : "");
}
return ($isMacro ? "" : "\\fP");
}
public function CDATA($str) {
return $this->highlight(trim($str), $this->role, 'troff');
}
public function TEXT($str) {
$ret = trim(preg_replace( '/[ \n\t]+/', ' ', $str));
// No newline if current line begins with ',', ';', ':', '.'
if (strncmp($ret, ",", 1) && strncmp($ret, ";", 1) && strncmp($ret, ":", 1) && strncmp($ret, ".", 1))
$ret = "\n" . $ret;
return $ret;
}
public function getChunkInfo() {
return $this->cchunk;
}
public function format_suppressed_tags($open, $name, $attrs) {
/* Ignore it */
return "";
}
public function format_suppressed_text($value, $tag) {
/* Suppress any content */
return "";
}
public function format_refsect_text($value, $tag) {
if ($this->cchunk["appendlater"] && isset($this->cchunk["buffer"]))
array_push($this->cchunk["buffer"], strtoupper('"'.$value.'"'));
else
return strtoupper('"'.$value.'"');
}
public function format_refsect_title($open, $name, $attrs, $props) {
if ($open) {
if ($this->cchunk["appendlater"] && isset($this->cchunk["buffer"]))
array_push($this->cchunk["buffer"], "\n.SH ");
else return "\n.SH ";
}
return "";
}
public function format_refpurpose($open, $name, $attrs, $props) {
if ($open) {
return " \- ";
}
}
public function format_function_text($value, $tag) {
return "\n\\fB" . $this->toValidName($value) . "\\fP(3)";
}
public function format_parameter_text($value, $tag) {
return "\n\\fI" . ((isset($value[0]) && $value[0] == "$") ? "" : "$") . $value . "\\fP";
}
public function format_parameter_method($open, $name, $attrs, $props) {
if ($open && isset($attrs[PhDReader::XMLNS_DOCBOOK]["role"]) && $attrs[PhDReader::XMLNS_DOCBOOK]["role"] == "reference") {
$this->cchunk['methodsynopsis']['params'][count($this->cchunk['methodsynopsis']['params'])-1]['reference'] = true;
}
return "";
}
public function format_parameter_term_text($value, $tag) {
return "\n\\fI$" . $value . "\\fP\n\-";
}
public function format_term($open, $name, $attrs, $props) {
if ($open)
return "";
return "\n\-";
}
public function format_simplelist($open, $name, $attrs, $props) {
if (isset($this->cchunk["role"]) && $this->cchunk["role"] == "seealso") {
if ($open) {
$this->cchunk["firstitem"] = true;
return "";
}
return ".";
} else {
if ($open) {
$this->cchunk["firstitem"] = true;
return "\n.PP\n.RS\n";
}
return "\n.RE\n.PP\n";
}
}
public function format_member($open, $name, $attrs, $props) {
if ($open) {
if (isset($this->cchunk["role"]) && $this->cchunk["role"] == "seealso") {
if ($this->cchunk["firstitem"]) {
$ret = "";
$this->cchunk["firstitem"] = false;
} else $ret = ",";
return $ret;
}
if ($this->cchunk["firstitem"]) {
$ret = "\n.TP 0.2i\n\\(bu";
$this->cchunk["firstitem"] = false;
} else $ret = "\n.TP 0.2i\n\\(bu";
return $ret;
}
return "";
}
public function format_admonition($open, $name, $attrs, $props) {
if ($open) {
return "\n.PP\n\\fB" . $this->autogen($name, $props["lang"]) . "\\fR\n.RS";
}
return "\n.RE\n.PP";
}
public function format_example($open, $name, $attrs, $props) {
if ($open && isset($this->cchunk["examplenumber"])) {
return "\n.PP\n\\fB" . $this->autogen($name, $props["lang"]) . ++$this->cchunk["examplenumber"] . "\\fR\n.RS";
}
return "\n.RE";
}
public function format_itemizedlist($open, $name, $attrs, $props) {
if ($open) {
return "\n.PP\n.RS";
}
return "\n.RE\n.PP";
}
public function format_methodparam($open, $name, $attrs, $props) {
if ($open) {
$opt = isset($attrs[PhDReader::XMLNS_DOCBOOK]["choice"]) &&
$attrs[PhDReader::XMLNS_DOCBOOK]["choice"] == "opt";
$this->cchunk["methodsynopsis"]["params"][] = array(
"optional" => $opt,
"type" => "",
"name" => "",
"initializer" => "",
"reference" => false,
);
}
return "";
}
public function format_void($open, $name, $attrs, $props) {
if ($open) {
$this->cchunk["methodsynopsis"]["params"][] = array(
"optional" => false,
"type" => "void",
"name" => "",
"initializer" => "",
"reference" => false,
);
}
return "";
}
public function format_type_method_text($value, $tag) {
$this->cchunk['methodsynopsis']['params'][count($this->cchunk['methodsynopsis']['params'])-1]['type'] = $value;
return "";
}
public function format_parameter_method_text($value, $tag) {
$this->cchunk['methodsynopsis']['params'][count($this->cchunk['methodsynopsis']['params'])-1]['name'] = $value;
return "";
}
public function format_initializer_method_text($value, $tag) {
$this->cchunk['methodsynopsis']['params'][count($this->cchunk['methodsynopsis']['params'])-1]['initializer'] = $value;
return "";
}
public function format_refsynopsisdiv($open, $name, $attrs, $props) {
if ($open && isset($this->cchunk["methodsynopsis"]["firstsynopsis"])
&& $this->cchunk["methodsynopsis"]["firstsynopsis"]) {
return "\n.SH SYNOPSIS\n";
}
if (!$open && isset($this->cchunk["methodsynopsis"]["firstsynopsis"]))
$this->cchunk["methodsynopsis"]["firstsynopsis"] = false;
return "";
}
public function format_methodsynopsis($open, $name, $attrs, $props) {
if ($open && isset($this->cchunk["methodsynopsis"]["firstsynopsis"])
&& $this->cchunk["methodsynopsis"]["firstsynopsis"] && $this->cchunk["appendlater"]) {
$this->cchunk["appendlater"] = false;
return "\n.SH SYNOPSIS\n";
}
if ($open)
return "\n.br";
$params = array();
// write the formatted synopsis
foreach ($this->cchunk['methodsynopsis']['params'] as $parameter) {
array_push($params, ($parameter['optional'] ? "[" : "")
. $parameter['type']
. ($parameter['reference'] ? " \\fI&\\fP" : " ")
. ($parameter['name'] ? "\\fI$" . $parameter['name'] . "\\fP" : "")
. ($parameter['initializer'] ? " = " . $parameter['initializer'] : "")
. ($parameter['optional'] ? "]" : "") );
}
$ret = "\n(" . join($params, ", ") . ")";
$this->cchunk['methodsynopsis']['params'] = array();
// finally write what is in the buffer
if (isset($this->cchunk["buffer"])) {
$ret .= implode("", $this->cchunk["buffer"]);
$this->cchunk["buffer"] = array();
}
if (isset($this->cchunk["methodsynopsis"]["firstsynopsis"]))
$this->cchunk["methodsynopsis"]["firstsynopsis"] = false;
return $ret;
}
public function newChunk() {
$this->cchunk = $this->dchunk;
$this->chunkOpen = true;
}
public function closeChunk() {
$this->chunkOpen = false;
}
public function format_xref($open, $name, $attrs, $props) {
if ($props['empty'])
return "\n\"" . PhDHelper::getDescription($attrs[PhDReader::XMLNS_DOCBOOK]["linkend"]) . "\"";
return "";
}
public function format_verbatim($open, $name, $attrs, $props) {
if ($open) {
if (isset($attrs[PhDReader::XMLNS_DOCBOOK]["role"])) {
$this->role = $attrs[PhDReader::XMLNS_DOCBOOK]["role"];
} else {
$this->role = false;
}
return "\n.PP\n.nf";
}
return "\n.fi";
}
public function format_verbatim_text($value, $tag) {
return "\n" . trim($value) . "\n";
}
public function format_refsect($open, $name, $attrs, $props) {
if ($open && isset($attrs[PhDReader::XMLNS_DOCBOOK]["role"])) {
$this->cchunk["role"] = $attrs[PhDReader::XMLNS_DOCBOOK]["role"];
if ($this->cchunk["role"] == "description") {
$this->cchunk["appendlater"] = true;
}
}
if (!$open)
$this->cchunk["role"] = null;
return "";
}
// Returns the unformatted value without whitespaces (nor new lines)
public function format_text($value, $tag) {
return trim(preg_replace('/[ \n\t]+/', ' ', $value));
}
public function format_tgroup($open, $name, $attrs, $props) {
if ($open) {
$nbCols = $attrs[PhDReader::XMLNS_DOCBOOK]["cols"];
$ret = "\n.TS\nbox, tab (|);\n";
for ($i = 0; $i < $nbCols; $i++)
$ret .= "c | ";
$ret .= ".";
return $ret;
}
return "\n.TE\n.PP";
}
public function format_thead($open, $name, $attrs, $props) {
if ($open) {
return "";
}
return "\n=";
}
public function format_row($open, $name, $attrs, $props) {
if ($open) {
$this->cchunk["firstitem"] = true;
return "\n";
}
return "";
}
public function format_entry($open, $name, $attrs, $props) {
if ($open) {
if ($this->cchunk["firstitem"]) {
$this->cchunk["firstitem"] = false;
return "T{\n";
}
return "|T{\n";
}
return "\nT}";
}
public function format_segmentedlist($open, $name, $attrs, $props) {
if ($open) {
return "\n.P";
}
$this->cchunk["segtitle"] = array();
return "\n";
}
public function format_seglistitem($open, $name, $attrs, $props) {
if ($open && isset($this->cchunk["segindex"]))
$this->cchunk["segindex"] = 0;
return "";
}
public function format_seg($open, $name, $attrs, $props) {
if (! (isset($this->cchunk["segtitle"]) && isset($this->cchunk["segtitle"][$this->cchunk["segindex"]])) )
return "";
if ($open) {
return "\n.br\n\\fB" . $this->cchunk["segtitle"][$this->cchunk["segindex"]] . ":\\fR";
}
$this->cchunk["segindex"]++;
return "";
}
public function format_segtitle_text($value, $tag) {
if (isset($this->cchunk["segtitle"]))
$this->cchunk["segtitle"][] = $value;
return "";
}
public function format_manvolnum($open, $name, $attrs, $props) {
if ($open) {
return "(";
}
return ")";
}
public function format_ooclass_name_text($value, $tag) {
return "\n.br\n\\fI" . $value . "\\fP";
}
public function format_indent($open, $name, $attrs, $props) {
if ($open) {
return "\n.PP\n.RS";
}
return "\n.RE\n.PP";
}
public function format_tag_text($value, $tag) {
return "\n<" . $value . ">";
}
// Convert the function name to a Unix valid filename
public function toValidName($functionName) {
return str_replace(array("::", "->", "()"), array(".", ".", ""), $functionName);
}
}
|