Common.pm 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636
  1. # Common.pm: definition of commands. Common code of other Texinfo modules.
  2. #
  3. # Copyright 2010, 2011, 2012 Free Software Foundation, Inc.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 3 of the License,
  8. # or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. # Original author: Patrice Dumas <pertusus@free.fr>
  19. # Parts (also from Patrice Dumas) come from texi2html.pl or texi2html.init.
  20. package Texinfo::Common;
  21. use strict;
  22. # for unicode/layer support in binmode
  23. use 5.006;
  24. # to determine the null file
  25. use Config;
  26. use File::Spec;
  27. use Texinfo::Documentlanguages;
  28. # debugging
  29. use Carp qw(cluck);
  30. require Exporter;
  31. use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  32. @ISA = qw(Exporter);
  33. # Items to export into callers namespace by default. Note: do not export
  34. # names by default without a very good reason. Use EXPORT_OK instead.
  35. # Do not simply export all your public functions/methods/constants.
  36. # This allows declaration use Texinfo::Covert::Text ':all';
  37. # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
  38. # will save memory.
  39. %EXPORT_TAGS = ( 'all' => [ qw(
  40. definition_category
  41. expand_verbatiminclude
  42. expand_today
  43. float_name_caption
  44. is_content_empty
  45. move_index_entries_after_items_in_tree
  46. normalize_top_node_name
  47. numbered_heading
  48. protect_comma_in_tree
  49. protect_first_parenthesis
  50. protect_hashchar_at_line_beginning
  51. protect_colon_in_tree
  52. protect_node_after_label_in_tree
  53. trim_spaces_comment_from_content
  54. valid_tree_transformation
  55. ) ] );
  56. @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
  57. @EXPORT = qw(
  58. );
  59. $VERSION = '5.1.90';
  60. # i18n
  61. sub N__($)
  62. {
  63. return $_[0];
  64. }
  65. # determine the null devices
  66. my $default_null_device = File::Spec->devnull();
  67. our %null_device_file = (
  68. $default_null_device => 1
  69. );
  70. # special case, djgpp recognizes both null devices
  71. if ($Config{osname} eq 'dos' and $Config{osvers} eq 'djgpp') {
  72. $null_device_file{'/dev/null'} = 1;
  73. $null_device_file{'NUL'} = 1;
  74. }
  75. # these are the default values for the parser state that may be
  76. # initialized to values given by the user.
  77. # They are defined here, because they are used below and we
  78. # don't want Texinfo::Common to use Texinfo::Parser.
  79. our %default_parser_state_configuration = (
  80. # this is the initial context. It is put at the bottom of the
  81. # 'context_stack'. It is not clear if this is really useful to be
  82. # able to customize that value.
  83. 'context' => '_root',
  84. 'expanded_formats' => [],
  85. 'gettext' => sub {return $_[0];},
  86. 'pgettext' => sub {return $_[1];},
  87. 'include_directories' => [ '.' ],
  88. # these are the user-added indices. May be an array reference on names
  89. # or an hash reference in the same format than %index_names below
  90. 'indices' => [],
  91. # the following are dynamically modified during the document parsing.
  92. 'aliases' => {}, # key is a command name value is the alias
  93. 'clickstyle' => 'arrow',
  94. 'documentlanguage' => undef,
  95. # Current documentlanguage set by
  96. # @documentlanguage
  97. 'explained_commands' => {}, # the key is a command name, either acronym
  98. # or abbr, the value is a hash. The key hash
  99. # is a normalized first argument of the
  100. # corresponding command, the value is the
  101. # contents array of the previous command with
  102. # this first arg and a second arg.
  103. 'kbdinputstyle' => 'distinct',
  104. 'labels' => {}, # keys are normalized label names, as described
  105. # in the `HTML Xref' node. Value should be
  106. # a node/anchor or float in the tree.
  107. 'macros' => {}, # the key is the user-defined macro name. The
  108. # value is the reference on a macro element
  109. # as obtained by parsing the @macro
  110. 'merged_indices' => {}, # the key is merged in the value
  111. 'novalidate' => 0, # same as setting @novalidate.
  112. 'sections_level' => 0, # modified by raise/lowersections
  113. 'values' => {'txicommandconditionals' => 1},
  114. # the key is the name, the value the @set name
  115. # argument. A Texinfo tree may also be used.
  116. );
  117. # command-line options
  118. #my @command_line_settable_at_commands = ('footnotestyle', 'novalidate',
  119. # 'documentlanguage', 'paragraphindent');
  120. # FIXME maybe this should better be set as texinfo passed to texi2any as
  121. # texi2dvi --command
  122. # customization options
  123. our %document_settable_at_commands = (
  124. 'allowcodebreaks' => 'true',
  125. 'clickstyle' => '@arrow',
  126. 'codequotebacktick' => 'off',
  127. 'codequoteundirected' => 'off',
  128. 'contents' => 0,
  129. 'deftypefnnewline' => 'off',
  130. 'documentencoding' => 'us-ascii',
  131. 'documentlanguage' => 'en',
  132. # is N ems in TeX, 0.4 in.
  133. 'exampleindent' => 5,
  134. 'firstparagraphindent' => 'none',
  135. 'frenchspacing' => 'off',
  136. 'headings' => 'on',
  137. 'kbdinputstyle' => 'distinct',
  138. 'paragraphindent' => 3,
  139. 'shortcontents' => 0,
  140. 'urefbreakstyle' => 'after',
  141. 'xrefautomaticsectiontitle' => 'off',
  142. );
  143. # those should be unique
  144. our %document_settable_unique_at_commands = (
  145. # when passed through a configuration variable, documentdescription
  146. # should be already formatted for HTML
  147. 'documentdescription' => undef,
  148. 'evenfootingmarks' => undef,
  149. 'evenheadingmarks' => undef,
  150. 'everyfootingmarks' => 'bottom',
  151. 'everyheadingmarks' => 'bottom',
  152. 'fonttextsize' => 11,
  153. 'footnotestyle' => 'end',
  154. 'novalidate' => 0,
  155. 'oddfootingmarks' => undef,
  156. 'oddheadingmarks' => undef,
  157. # FIXME not clear here.
  158. 'pagesizes' => undef,
  159. 'setchapternewpage' => 'on',
  160. 'setcontentsaftertitlepage' => 0,
  161. 'setfilename' => undef,
  162. 'setshortcontentsaftertitlepage' => 0,
  163. 'everyheading' => undef,
  164. 'everyfooting' => undef,
  165. 'evenheading' => undef,
  166. 'evenfooting' => undef,
  167. 'oddheading' => undef,
  168. 'oddfooting' => undef,
  169. );
  170. my @command_line_settables = ('FILLCOLUMN', 'SPLIT', 'SPLIT_SIZE',
  171. 'HEADERS',
  172. 'MACRO_EXPAND', 'NUMBER_SECTIONS',
  173. 'NUMBER_FOOTNOTES', 'NODE_FILES',
  174. 'NO_WARN', 'VERBOSE',
  175. 'TRANSLITERATE_FILE_NAMES', 'ERROR_LIMIT', 'ENABLE_ENCODING',
  176. 'FORCE', 'INTERNAL_LINKS', 'OUTFILE', 'SUBDIR', 'OUT',
  177. 'SILENT', 'CASE_INSENSITIVE_FILENAMES',
  178. );
  179. # documented in the Texinfo::Parser pod section
  180. # all are lower cased in texi2any.pl
  181. my @parser_options = map {uc($_)} (keys(%default_parser_state_configuration));
  182. my @obsolete_variables = ('TOP_HEADING_AT_BEGINNING', 'USE_SECTIONS',
  183. 'IDX_SUMMARY', 'I18N_PERL_HASH', 'USE_UNICODE', 'USE_NLS',
  184. 'USE_UP_FOR_ADJACENT_NODES', 'SEPARATE_DESCRIPTION',
  185. 'NEW_CROSSREF_STYLE', 'SHORT_REF', 'IGNORE_PREAMBLE_TEXT',
  186. 'OUT_ENCODING',
  187. 'IN_ENCODING', 'DEFAULT_ENCODING');
  188. my @variable_settables_not_used = ('COMPLETE_IMAGE_PATHS', 'TOC_FILE',
  189. 'SPLIT_INDEX');
  190. my @formats_settable = (
  191. );
  192. my @variable_string_settables = (
  193. 'DEBUG', 'FRAMES', 'FRAMESET_DOCTYPE', 'DOCTYPE', 'TEST', 'DUMP_TEXI',
  194. 'TOP_FILE', 'SHOW_MENU', 'USE_NODES', 'TOC_LINKS', 'SHORTEXTN',
  195. 'PREFIX', 'DEF_TABLE', 'L2H', 'MONOLITHIC',
  196. 'L2H_L2H', 'L2H_SKIP', 'L2H_TMP', 'L2H_FILE', 'L2H_CLEAN',
  197. 'L2H_HTML_VERSION', 'EXTERNAL_DIR', 'USE_ISO',
  198. 'VERTICAL_HEAD_NAVIGATION', 'INLINE_CONTENTS', 'NODE_FILE_EXTENSION',
  199. 'NO_CSS', 'INLINE_CSS_STYLE', 'USE_TITLEPAGE_FOR_TITLE',
  200. 'SIMPLE_MENU', 'EXTENSION', 'INLINE_INSERTCOPYING', 'USE_NUMERIC_ENTITY',
  201. 'ENABLE_ENCODING_USE_ENTITY', 'ICONS',
  202. 'USE_UNIDECODE', 'DATE_IN_HEADER', 'OPEN_QUOTE_SYMBOL',
  203. 'CLOSE_QUOTE_SYMBOL', 'TOP_NODE_UP', 'TOP_NODE_UP_URL', 'TOP_NODE_FILE',
  204. 'TOP_NODE_FILE_TARGET', 'SHOW_TITLE', 'WORDS_IN_PAGE',
  205. 'HEADER_IN_TABLE', 'USE_ACCESSKEY', 'USE_REL_REV', 'USE_LINKS',
  206. 'OVERVIEW_LINK_TO_TOC', 'AVOID_MENU_REDUNDANCY', 'NODE_NAME_IN_MENU',
  207. 'NODE_NAME_IN_INDEX', 'NO_USE_SETFILENAME', 'USE_SETFILENAME_EXTENSION',
  208. 'COMPLEX_FORMAT_IN_TABLE',
  209. 'IGNORE_BEFORE_SETFILENAME', 'IGNORE_SPACE_AFTER_BRACED_COMMAND_NAME',
  210. 'USE_NODE_TARGET',
  211. 'PROGRAM_NAME_IN_FOOTER', 'NODE_FILENAMES',
  212. 'EXTERNAL_CROSSREF_SPLIT', 'BODYTEXT',
  213. 'CSS_LINES', 'RENAMED_NODES_REDIRECTIONS', 'RENAMED_NODES_FILE',
  214. 'CPP_LINE_DIRECTIVES',
  215. 'TEXI2DVI', 'DUMP_TREE', 'MAX_MACRO_CALL_NESTING',
  216. 'INPUT_ENCODING_NAME', 'INPUT_PERL_ENCODING',
  217. 'OUTPUT_ENCODING_NAME', 'OUTPUT_PERL_ENCODING',
  218. 'PACKAGE_VERSION',
  219. 'PACKAGE_AND_VERSION', 'PACKAGE_URL', 'PACKAGE', 'PACKAGE_NAME', 'PROGRAM',
  220. 'PRE_BODY_CLOSE', 'AFTER_BODY_OPEN', 'PRE_ABOUT', 'AFTER_ABOUT',
  221. 'EXTRA_HEAD', 'DO_ABOUT',
  222. 'DEFAULT_RULE', 'BIG_RULE',
  223. 'MENU_ENTRY_COLON', 'INDEX_ENTRY_COLON', 'MENU_SYMBOL',
  224. 'MAX_HEADER_LEVEL', 'CHAPTER_HEADER_LEVEL',
  225. 'FOOTNOTE_END_HEADER_LEVEL', 'FOOTNOTE_SEPARATE_HEADER_LEVEL',
  226. 'USE_UP_NODE_FOR_ELEMENT_UP',
  227. 'BEFORE_OVERVIEW', 'AFTER_OVERVIEW',
  228. 'BEFORE_TOC_LINES', 'AFTER_TOC_LINES',
  229. 'SORT_ELEMENT_COUNT', 'SORT_ELEMENT_COUNT_WORDS',
  230. 'KEEP_TOP_EXTERNAL_REF',
  231. 'TEXI2HTML', 'IMAGE_LINK_PREFIX', 'FIX_TEXINFO',
  232. 'TREE_TRANSFORMATIONS', 'BASEFILENAME_LENGTH',
  233. 'TEXTCONTENT_COMMENT', 'XREF_USE_FLOAT_LABEL', 'XREF_USE_NODE_NAME_ARG',
  234. 'MACRO_BODY_IGNORES_LEADING_SPACE', 'CHECK_HTMLXREF',
  235. 'TEXINFO_DTD_VERSION', 'TEXINFO_COLUMN_FOR_DESCRIPTION',
  236. 'TEXINFO_OUTPUT_FORMAT', 'INFO_SPECIAL_CHARS_WARNING',
  237. 'INDEX_SPECIAL_CHARS_WARNING',
  238. );
  239. # Not strings.
  240. # FIXME To be documented somewhere, but where?
  241. my @variable_other_settables = (
  242. 'LINKS_BUTTONS', 'TOP_BUTTONS', 'SECTION_BUTTONS', 'BUTTONS_TEXT',
  243. 'BUTTONS_ACCESSKEY', 'BUTTONS_REL', 'BUTTONS_GOTO',
  244. 'CHAPTER_FOOTER_BUTTONS', 'SECTION_FOOTER_BUTTONS',
  245. 'NODE_FOOTER_BUTTONS',
  246. 'MISC_BUTTONS', 'CHAPTER_BUTTONS', 'BUTTONS_NAME',
  247. 'BUTTONS_EXAMPLE', 'SPECIAL_ELEMENTS_NAME', 'SPECIAL_ELEMENTS_CLASS',
  248. 'ACTIVE_ICONS', 'PASSIVE_ICONS',
  249. 'CSS_FILES', 'CSS_REFS',
  250. 'GLOBAL_COMMANDS',
  251. );
  252. my %valid_options;
  253. foreach my $var (keys(%document_settable_at_commands),
  254. keys(%document_settable_unique_at_commands),
  255. @command_line_settables, @variable_string_settables,
  256. @variable_other_settables, @parser_options,
  257. @formats_settable,
  258. @obsolete_variables, @variable_settables_not_used) {
  259. $valid_options{$var} = 1;
  260. }
  261. my %obsolete_options;
  262. foreach my $var (@obsolete_variables) {
  263. $obsolete_options{$var} = 1;
  264. }
  265. sub valid_option($)
  266. {
  267. my $option = shift;
  268. return $valid_options{$option};
  269. }
  270. sub obsolete_option($)
  271. {
  272. my $option = shift;
  273. return $obsolete_options{$option};
  274. }
  275. my %customization_variable_classes = (
  276. 'document_settable_at_commands' => [ sort(keys(%document_settable_at_commands)) ],
  277. 'document_settable_unique_at_commands' => [ sort(keys(%document_settable_unique_at_commands)) ],
  278. 'command_line_settables' => \@command_line_settables,
  279. 'variable_string_settables' => \@variable_string_settables,
  280. 'variable_other_settables' => \@variable_other_settables,
  281. 'parser_options' => \@parser_options,
  282. #'formats_settable' => \@formats_settable,
  283. 'obsolete_variables' => \@obsolete_variables,
  284. 'variable_settables_not_used' => \@variable_settables_not_used,
  285. );
  286. my @secondary_customization_variables = (
  287. 'obsolete_variables', 'variable_settables_not_used'
  288. );
  289. sub _customization_variable_classes(;$)
  290. {
  291. my $print_all = shift;
  292. my $result = '';
  293. foreach my $type (sort(keys(%customization_variable_classes))) {
  294. next if (!$print_all
  295. and grep {$_ eq $type} @secondary_customization_variables);
  296. foreach my $variable (@{$customization_variable_classes{$type}}) {
  297. $result .= "$variable\t$type\n";
  298. }
  299. }
  300. return $result;
  301. }
  302. my %valid_tree_transformations;
  303. foreach my $valid_transformation ('simple_menus',
  304. 'fill_gaps_in_sectioning', 'move_index_entries_after_items',
  305. 'insert_nodes_for_sectioning_commands',
  306. 'complete_tree_nodes_menus', 'regenerate_master_menu',
  307. 'indent_menu_descriptions') {
  308. $valid_tree_transformations{$valid_transformation} = 1;
  309. }
  310. sub valid_tree_transformation ($)
  311. {
  312. my $transformation = shift;
  313. return 1 if (defined($transformation)
  314. and $valid_tree_transformations{$transformation});
  315. return 0;
  316. }
  317. our %no_brace_commands; # commands never taking braces
  318. %no_brace_commands = (
  319. '*', "\n",
  320. ' ', ' ',
  321. "\t", ' ',
  322. "\n", ' ',
  323. '-', '', # hyphenation hint
  324. '|', '', # used in formatting commands @evenfooting and friends
  325. '/', '',
  326. ':', '',
  327. '!', '!',
  328. '?', '?',
  329. '.', '.',
  330. '@', '@',
  331. '}', '}',
  332. '{', '{',
  333. '\\', '\\', # should only appear in math
  334. );
  335. # commands taking a line as argument or no argument.
  336. # sectioning commands and def* commands are added below.
  337. # index commands are added dynamically.
  338. #
  339. # The values signification is:
  340. # special: no value and macro expansion, all the line is used, and
  341. # analysed during parsing (_parse_special_misc_command)
  342. # lineraw: no value and macro expansion, the line is kept as-is, not
  343. # analysed
  344. # skipline: no argument, everything else on the line is skipped
  345. # skipspace: no argument, following spaces are skipped.
  346. # noarg: no argument
  347. # text: the line is parsed as texinfo, and the argument is converted
  348. # to simple text (in _end_line)
  349. # line: the line is parsed as texinfo
  350. # a number: the line is parsed as texinfo and the result should be plain
  351. # text maybe followed by a comment; the result is analysed
  352. # during parsing (_parse_line_command_args).
  353. # The number is an indication of the number of arguments of
  354. # the command.
  355. #
  356. # Beware that @item and @itemx may be like 'line' or 'skipspace' depending
  357. # on the context.
  358. our %misc_commands = (
  359. 'node' => 'line', # special arg
  360. 'bye' => 'skipline', # no arg
  361. 'end' => 'text',
  362. # set, clear
  363. 'set' => 'special', # special arg
  364. 'clear' => 'special', # special arg
  365. 'unmacro' => 'special',
  366. # comments
  367. 'comment' => 'lineraw',
  368. 'c' => 'lineraw',
  369. # special
  370. 'definfoenclose' => 3,
  371. 'alias' => 2,
  372. # number of arguments is not known in advance.
  373. 'columnfractions' => 1,
  374. # file names
  375. 'setfilename' => 'text',
  376. 'verbatiminclude' => 'text',
  377. 'include' => 'text',
  378. 'raisesections' => 'skipline', # no arg
  379. 'lowersections' => 'skipline', # no arg
  380. 'contents' => 'skipline', # no arg
  381. 'shortcontents' => 'skipline', # no arg
  382. 'summarycontents' => 'skipline', # no arg
  383. 'insertcopying' => 'noarg', # no arg
  384. 'clickstyle' => 'special', # arg should be an @-command
  385. # more relevant in preamble
  386. 'setcontentsaftertitlepage' => 'skipline', # no arg
  387. 'setshortcontentsaftertitlepage' => 'skipline', # no arg
  388. 'documentencoding' => 'text', # or 1?
  389. 'novalidate' => 'skipline', # no arg
  390. 'dircategory' => 'line', # line. Position with regard
  391. # with direntry is significant
  392. 'pagesizes' => 'line', # can have 2 args
  393. # or one? 200mm,150mm 11.5in
  394. 'finalout' => 'skipline', # no arg
  395. 'paragraphindent' => 1, # arg none asis
  396. # or a number and forbids anything else on the line
  397. 'firstparagraphindent' => 1, # none insert
  398. 'frenchspacing' => 1, # on off
  399. 'codequoteundirected' => 1, # on off
  400. 'codequotebacktick' => 1, # on off
  401. 'xrefautomaticsectiontitle' => 1, # on off
  402. 'deftypefnnewline' => 1, # on off
  403. 'fonttextsize' => 1, # 10 11
  404. 'allowcodebreaks' => 1, # false or true
  405. 'exampleindent' => 1, # asis or a number
  406. 'footnotestyle' => 1, # end and separate, nothing else on the line
  407. 'urefbreakstyle' => 1, # after|before|none
  408. 'afourpaper' => 'skipline', # no arg
  409. 'afivepaper' => 'skipline', # no arg
  410. 'afourlatex' => 'skipline', # no arg
  411. 'afourwide' => 'skipline', # no arg
  412. 'headings' => 1, #off on single double singleafter doubleafter
  413. # interacts with setchapternewpage
  414. 'setchapternewpage' => 1, # off on odd
  415. # only relevant in TeX, and special
  416. 'everyheading' => 'lineraw', # @*heading @*footing use @|
  417. 'everyfooting' => 'lineraw', # + @thispage @thissectionname
  418. 'evenheading' => 'lineraw', # @thissectionnum @thissection
  419. 'evenfooting' => 'lineraw', # @thischaptername @thischapternum
  420. 'oddheading' => 'lineraw', # @thischapter @thistitle @thisfile
  421. 'oddfooting' => 'lineraw',
  422. 'smallbook' => 'skipline', # no arg
  423. 'syncodeindex' => 2, # args are index identifiers
  424. 'synindex' => 2,
  425. 'defindex' => 1, # one identifier arg
  426. 'defcodeindex' => 1, # one identifier arg
  427. 'documentlanguage' => 'text', # language code arg
  428. 'kbdinputstyle' => 1, # code example distinct
  429. 'everyheadingmarks' => 1, # top bottom
  430. 'everyfootingmarks' => 1,
  431. 'evenheadingmarks' => 1,
  432. 'oddheadingmarks' => 1,
  433. 'evenfootingmarks' => 1,
  434. 'oddfootingmarks' => 1,
  435. # not valid for info (should be in @iftex)
  436. 'cropmarks' => 'skipline', # no arg
  437. # formatting
  438. 'center' => 'line',
  439. 'printindex' => 1,
  440. 'listoffloats' => 'line',
  441. # especially in titlepage
  442. # 'shorttitle' => 'line',
  443. 'shorttitlepage' => 'line',
  444. 'settitle' => 'line',
  445. 'author' => 'line',
  446. 'subtitle' => 'line',
  447. 'title' => 'line',
  448. 'sp' => 1, # numerical arg
  449. 'page' => 'skipline', # no arg (pagebreak)
  450. 'need' => 1, # one numerical/real arg
  451. # formatting
  452. 'noindent' => 'skipspace', # no arg
  453. 'indent' => 'skipspace',
  454. 'exdent' => 'line',
  455. 'headitem' => 'skipspace',
  456. 'item' => 'skipspace', # or line, depending on the context
  457. 'itemx' => 'skipspace', # or line, depending on the context
  458. 'tab' => 'skipspace',
  459. # only valid in heading or footing
  460. 'thischapter' => 'noarg',
  461. 'thischaptername' => 'noarg',
  462. 'thischapternum' => 'noarg',
  463. 'thisfile' => 'noarg',
  464. 'thispage' => 'noarg',
  465. 'thistitle' => 'noarg',
  466. # not valid for info (should be in @iftex)
  467. 'vskip' => 'lineraw', # arg line in TeX
  468. # obsolete @-commands.
  469. 'refill' => 'noarg', # no arg (obsolete, to be ignored)
  470. # Remove spaces and end of lines after the
  471. # commands? If no, they can lead to empty lines
  472. 'quote-arg' => 'skipline',
  473. 'allow-recursion' => 'skipline',
  474. );
  475. # key is index name, keys of the reference value are the prefixes.
  476. # value associated with the prefix is 0 if the prefix is not a code-like
  477. # prefix, 1 if it is a code-like prefix (set by defcodeindex/syncodeindex).
  478. #our %index_names = (
  479. # 'cp' => {'cp' => 0, 'c' => 0},
  480. # 'fn' => {'fn' => 1, 'f' => 1},
  481. # 'vr' => {'vr' => 1, 'v' => 1},
  482. # 'ky' => {'ky' => 1, 'k' => 1},
  483. # 'pg' => {'pg' => 1, 'p' => 1},
  484. # 'tp' => {'tp' => 1, 't' => 1}
  485. #);
  486. our %index_names = (
  487. 'cp' => {'prefix' => ['c'], 'in_code' => 0},
  488. 'fn' => {'prefix' => ['f'], 'in_code' => 1},
  489. 'vr' => {'prefix' => ['v'], 'in_code' => 1},
  490. 'ky' => {'prefix' => ['k'], 'in_code' => 1},
  491. 'pg' => {'prefix' => ['p'], 'in_code' => 1},
  492. 'tp' => {'prefix' => ['t'], 'in_code' => 1},
  493. );
  494. foreach my $index(keys(%index_names)) {
  495. $index_names{$index}->{'name'} = $index;
  496. push @{$index_names{$index}->{'prefix'}}, $index;
  497. }
  498. our %default_index_commands;
  499. # all the commands are readded dynamically in the Parser.
  500. foreach my $index_name (keys (%index_names)) {
  501. foreach my $index_prefix (@{$index_names{$index_name}->{'prefix'}}) {
  502. next if ($index_prefix eq $index_name);
  503. # only put the one letter versions in the hash.
  504. $misc_commands{$index_prefix.'index'} = 'line';
  505. $default_index_commands{$index_prefix.'index'} = 1;
  506. }
  507. }
  508. # command with braces. Value is the max number of arguments.
  509. our %brace_commands;
  510. our %letter_no_arg_commands;
  511. foreach my $letter_no_arg_command ('aa','AA','ae','oe','AE','OE','o','O',
  512. 'ss','l','L','DH','dh','TH','th') {
  513. $letter_no_arg_commands{$letter_no_arg_command} = 1;
  514. $brace_commands{$letter_no_arg_command} = 0;
  515. }
  516. foreach my $no_arg_command ('TeX','LaTeX','bullet','copyright',
  517. 'registeredsymbol','dots','enddots','equiv','error','expansion','arrow',
  518. 'minus','point','print','result','today',
  519. 'exclamdown','questiondown','pounds','ordf','ordm',
  520. 'atchar', 'lbracechar', 'rbracechar', 'backslashchar', 'hashchar', 'comma',
  521. 'euro', 'geq','leq','tie','textdegree','click',
  522. 'quotedblleft','quotedblright','quoteleft','quoteright','quotedblbase',
  523. 'quotesinglbase','guillemetleft','guillemetright','guillemotleft',
  524. 'guillemotright','guilsinglleft','guilsinglright') {
  525. $brace_commands{$no_arg_command} = 0;
  526. }
  527. # accent commands. They may be called with and without braces.
  528. our %accent_commands;
  529. foreach my $accent_command ('"','~','^','`',"'",',','=',
  530. 'ringaccent','H','dotaccent','u','ubaraccent',
  531. 'udotaccent','v','ogonek','tieaccent', 'dotless') {
  532. $accent_commands{$accent_command} = 1;
  533. $brace_commands{$accent_command} = 1;
  534. }
  535. our %style_commands;
  536. foreach my $style_command ('asis','cite','clicksequence',
  537. 'dfn', 'emph',
  538. 'sc', 't', 'var',
  539. 'headitemfont', 'code', 'command', 'env', 'file', 'kbd',
  540. 'option', 'samp', 'strong') {
  541. $brace_commands{$style_command} = 1;
  542. $style_commands{$style_command} = 1;
  543. }
  544. our %regular_font_style_commands;
  545. foreach my $command ('r', 'i', 'b', 'sansserif', 'slanted') {
  546. $regular_font_style_commands{$command} = 1;
  547. $brace_commands{$command} = 1;
  548. $style_commands{$command} = 1;
  549. }
  550. foreach my $one_arg_command (
  551. 'ctrl','dmn', 'w', 'key',
  552. 'titlefont','hyphenation','anchor','errormsg') {
  553. $brace_commands{$one_arg_command} = 1;
  554. }
  555. our %code_style_commands;
  556. foreach my $command ('code', 'command', 'env', 'file', 'kbd', 'key', 'option',
  557. 'samp', 'indicateurl', 'verb', 't') {
  558. $code_style_commands{$command} = 1;
  559. $brace_commands{$command} = 1;
  560. }
  561. # Commands that enclose full texts
  562. our %context_brace_commands;
  563. foreach my $context_brace_command ('footnote', 'caption', 'shortcaption', 'math') {
  564. $context_brace_commands{$context_brace_command} = $context_brace_command;
  565. $brace_commands{$context_brace_command} = 1;
  566. }
  567. our %explained_commands;
  568. foreach my $explained_command ('abbr', 'acronym') {
  569. $explained_commands{$explained_command} = 1;
  570. $brace_commands{$explained_command} = 2;
  571. }
  572. our %inline_format_commands;
  573. our %inline_commands;
  574. foreach my $inline_format_command ('inlineraw', 'inlinefmt',
  575. 'inlinefmtifelse') {
  576. $inline_format_commands{$inline_format_command} = 1;
  577. $brace_commands{$inline_format_command} = 2;
  578. $inline_commands{$inline_format_command} = 1;
  579. }
  580. $brace_commands{'inlinefmtifelse'} = 3;
  581. our %inline_conditional_commands;
  582. foreach my $inline_conditional_command ('inlineifclear', 'inlineifset') {
  583. $inline_conditional_commands{$inline_conditional_command} = 1;
  584. $brace_commands{$inline_conditional_command} = 2;
  585. $inline_commands{$inline_conditional_command} = 1;
  586. }
  587. # 'inlineset', 'inlineclear'
  588. #$brace_commands{'inlineclear'} = 1;
  589. foreach my $two_arg_command('email') {
  590. $brace_commands{$two_arg_command} = 2;
  591. }
  592. foreach my $three_arg_command('uref','url','inforef') {
  593. $brace_commands{$three_arg_command} = 3;
  594. }
  595. foreach my $five_arg_command('xref','ref','pxref','image') {
  596. $brace_commands{$five_arg_command} = 5;
  597. }
  598. # some classification to help converters
  599. our %ref_commands;
  600. foreach my $ref_command ('xref','ref','pxref','inforef') {
  601. $ref_commands{$ref_command} = 1;
  602. }
  603. our %in_heading_commands;
  604. foreach my $in_heading_command ('thischapter', 'thischaptername',
  605. 'thischapternum', 'thisfile', 'thispage', 'thistitle') {
  606. $in_heading_commands{$in_heading_command} = 1;
  607. }
  608. # brace command that is not replaced with text.
  609. my %unformatted_brace_commands;
  610. foreach my $unformatted_brace_command ('anchor', 'shortcaption',
  611. 'caption', 'hyphenation', 'errormsg') {
  612. $unformatted_brace_commands{$unformatted_brace_command} = 1;
  613. }
  614. # commands delimiting blocks, with an @end.
  615. # Value is either the number of arguments on the line separated by
  616. # commas or the type of command, 'raw', 'def' or 'multitable'.
  617. our %block_commands;
  618. # commands that have a possible content before an item
  619. our %block_item_commands;
  620. sub gdt($)
  621. {
  622. return $_[0];
  623. }
  624. our %def_map = (
  625. # basic commands.
  626. # 'arg' and 'argtype' are for everything appearing after the other
  627. # arguments.
  628. 'deffn', [ 'category', 'name', 'arg' ],
  629. 'defvr', [ 'category', 'name' ],
  630. 'deftypefn', [ 'category', 'type', 'name', 'argtype' ],
  631. 'deftypeop', [ 'category', 'class' , 'type', 'name', 'argtype' ],
  632. 'deftypevr', [ 'category', 'type', 'name' ],
  633. 'defcv', [ 'category', 'class' , 'name' ],
  634. 'deftypecv', [ 'category', 'class' , 'type', 'name' ],
  635. 'defop', [ 'category', 'class' , 'name', 'arg' ],
  636. 'deftp', [ 'category', 'name', 'argtype' ],
  637. # shortcuts
  638. 'defun', {'deffn' => gdt('Function')},
  639. 'defmac', {'deffn' => gdt('Macro')},
  640. 'defspec', {'deffn' => '{'.gdt('Special Form').'}'},
  641. 'defvar', {'defvr' => gdt('Variable')},
  642. 'defopt', {'defvr' => '{'.gdt('User Option').'}'},
  643. 'deftypefun', {'deftypefn' => gdt('Function')},
  644. 'deftypevar', {'deftypevr' => gdt('Variable')},
  645. 'defivar', {'defcv' => '{'.gdt('Instance Variable').'}'},
  646. 'deftypeivar', {'deftypecv' => '{'.gdt('Instance Variable').'}'},
  647. 'defmethod', {'defop' => gdt('Method')},
  648. 'deftypemethod', {'deftypeop' => gdt('Method')},
  649. );
  650. # the type of index, fn: function, vr: variable, tp: type
  651. my %index_type_def = (
  652. 'fn' => ['deffn', 'deftypefn', 'deftypeop', 'defop'],
  653. 'vr' => ['defvr', 'deftypevr', 'defcv', 'deftypecv' ],
  654. 'tp' => ['deftp']
  655. );
  656. our %command_index;
  657. $command_index{'vtable'} = 'vr';
  658. $command_index{'ftable'} = 'fn';
  659. foreach my $index_type (keys %index_type_def) {
  660. foreach my $def (@{$index_type_def{$index_type}}) {
  661. $command_index{$def} = $index_type;
  662. }
  663. }
  664. our %def_commands;
  665. our %def_aliases;
  666. foreach my $def_command(keys %def_map) {
  667. if (ref($def_map{$def_command}) eq 'HASH') {
  668. my ($real_command) = keys (%{$def_map{$def_command}});
  669. $command_index{$def_command} = $command_index{$real_command};
  670. $def_aliases{$def_command} = $real_command;
  671. }
  672. $block_commands{$def_command} = 'def';
  673. $misc_commands{$def_command.'x'} = 'line';
  674. $def_commands{$def_command} = 1;
  675. $def_commands{$def_command.'x'} = 1;
  676. $command_index{$def_command.'x'} = $command_index{$def_command};
  677. }
  678. #print STDERR "".Data::Dumper->Dump([\%def_aliases]);
  679. #print STDERR "".Data::Dumper->Dump([\%def_prepended_content]);
  680. $block_commands{'multitable'} = 'multitable';
  681. $block_item_commands{'multitable'} = 1;
  682. # block commands in which menu entry and menu comments appear
  683. our %menu_commands;
  684. foreach my $menu_command ('menu', 'detailmenu', 'direntry') {
  685. $menu_commands{$menu_command} = 1;
  686. $block_commands{$menu_command} = 0;
  687. };
  688. our %align_commands;
  689. foreach my $align_command('raggedright', 'flushleft', 'flushright') {
  690. $block_commands{$align_command} = 0;
  691. $align_commands{$align_command} = 1;
  692. }
  693. $align_commands{'center'} = 1;
  694. foreach my $block_command(
  695. 'cartouche', 'group', 'indentedblock', 'smallindentedblock') {
  696. $block_commands{$block_command} = 0;
  697. }
  698. our %region_commands;
  699. foreach my $block_command('titlepage', 'copying', 'documentdescription') {
  700. $block_commands{$block_command} = 0;
  701. $region_commands{$block_command} = 1;
  702. }
  703. our %preformatted_commands;
  704. our %preformatted_code_commands;
  705. foreach my $preformatted_command(
  706. 'example', 'smallexample', 'lisp', 'smalllisp') {
  707. $block_commands{$preformatted_command} = 0;
  708. $preformatted_commands{$preformatted_command} = 1;
  709. $preformatted_code_commands{$preformatted_command} = 1;
  710. }
  711. foreach my $preformatted_command(
  712. 'display', 'smalldisplay', 'format', 'smallformat') {
  713. $block_commands{$preformatted_command} = 0;
  714. $preformatted_commands{$preformatted_command} = 1;
  715. }
  716. our %format_raw_commands;
  717. foreach my $format_raw_command('html', 'tex', 'xml', 'docbook') {
  718. $block_commands{$format_raw_command} = 0;
  719. $format_raw_commands{$format_raw_command} = 1;
  720. }
  721. our %raw_commands;
  722. # macro/rmacro are special
  723. foreach my $raw_command ('verbatim',
  724. 'ignore', 'macro', 'rmacro') {
  725. $block_commands{$raw_command} = 'raw';
  726. $raw_commands{$raw_command} = 1;
  727. }
  728. our %texinfo_output_formats;
  729. foreach my $command (keys(%format_raw_commands), 'info', 'plaintext') {
  730. $block_commands{'if' . $command} = 'conditional';
  731. $block_commands{'ifnot' . $command} = 'conditional';
  732. $texinfo_output_formats{$command} = $command;
  733. }
  734. $block_commands{'ifset'} = 'conditional';
  735. $block_commands{'ifclear'} = 'conditional';
  736. $block_commands{'ifcommanddefined'} = 'conditional';
  737. $block_commands{'ifcommandnotdefined'} = 'conditional';
  738. # 'macro' ?
  739. foreach my $block_command_one_arg('table', 'ftable', 'vtable',
  740. 'itemize', 'enumerate', 'quotation', 'smallquotation') {
  741. $block_commands{$block_command_one_arg} = 1;
  742. $block_item_commands{$block_command_one_arg} = 1
  743. unless ($block_command_one_arg =~ /quotation/);
  744. }
  745. $block_commands{'float'} = 2;
  746. # commands that forces closing an opened paragraph.
  747. our %close_paragraph_commands;
  748. foreach my $block_command (keys(%block_commands)) {
  749. $close_paragraph_commands{$block_command} = 1
  750. unless ($block_commands{$block_command} eq 'raw' or
  751. $block_commands{$block_command} eq 'conditional'
  752. or $format_raw_commands{$block_command});
  753. }
  754. $close_paragraph_commands{'verbatim'} = 1;
  755. foreach my $close_paragraph_command ('titlefont', 'insertcopying', 'sp',
  756. 'verbatiminclude', 'page', 'item', 'itemx', 'tab', 'headitem',
  757. 'printindex', 'listoffloats', 'center', 'dircategory', 'contents',
  758. 'shortcontents', 'summarycontents', 'caption', 'shortcaption',
  759. 'setfilename', 'exdent') {
  760. $close_paragraph_commands{$close_paragraph_command} = 1;
  761. }
  762. foreach my $close_paragraph_command (keys(%def_commands)) {
  763. $close_paragraph_commands{$close_paragraph_command} = 1;
  764. }
  765. our %item_container_commands;
  766. foreach my $item_container_command ('itemize', 'enumerate') {
  767. $item_container_commands{$item_container_command} = 1;
  768. }
  769. our %item_line_commands;
  770. foreach my $item_line_command ('table', 'ftable', 'vtable') {
  771. $item_line_commands{$item_line_command} = 1;
  772. }
  773. our %deprecated_commands = (
  774. 'ctrl' => '',
  775. 'allow-recursion' => N__('recursion is always allowed'),
  776. 'quote-arg' => N__('arguments are quoted by default'),
  777. );
  778. my %unformatted_block_commands;
  779. foreach my $unformatted_block_command ('ignore', 'macro', 'rmacro') {
  780. $unformatted_block_commands{$unformatted_block_command} = 1;
  781. }
  782. # commands that should only appear at the root level and contain up to
  783. # the next root command. @node and sectioning commands.
  784. our %root_commands;
  785. our %command_structuring_level = (
  786. 'top', 0,
  787. 'chapter', 1,
  788. 'unnumbered', 1,
  789. 'chapheading', 1,
  790. 'appendix', 1,
  791. 'section', 2,
  792. 'unnumberedsec', 2,
  793. 'heading', 2,
  794. 'appendixsec', 2,
  795. 'subsection', 3,
  796. 'unnumberedsubsec', 3,
  797. 'subheading', 3,
  798. 'appendixsubsec', 3,
  799. 'subsubsection', 4,
  800. 'unnumberedsubsubsec', 4,
  801. 'subsubheading', 4,
  802. 'appendixsubsubsec', 4,
  803. );
  804. our %level_to_structuring_command;
  805. {
  806. my $sections = [ ];
  807. my $appendices = [ ];
  808. my $unnumbered = [ ];
  809. my $headings = [ ];
  810. foreach my $command (keys (%command_structuring_level)) {
  811. if ($command =~ /^appendix/) {
  812. $level_to_structuring_command{$command} = $appendices;
  813. } elsif ($command =~ /^unnumbered/ or $command eq 'top') {
  814. $level_to_structuring_command{$command} = $unnumbered;
  815. } elsif ($command =~ /section$/ or $command eq 'chapter') {
  816. $level_to_structuring_command{$command} = $sections;
  817. } else {
  818. $level_to_structuring_command{$command} = $headings;
  819. }
  820. $level_to_structuring_command{$command}->[$command_structuring_level{$command}]
  821. = $command;
  822. }
  823. $level_to_structuring_command{'appendixsection'} = $appendices;
  824. $level_to_structuring_command{'majorheading'} = $headings;
  825. $level_to_structuring_command{'centerchap'} = $unnumbered;
  826. }
  827. # out of the main hierarchy
  828. $command_structuring_level{'part'} = 0;
  829. # this are synonyms
  830. $command_structuring_level{'appendixsection'} = 2;
  831. # command_structuring_level{'majorheading'} is also 1 and not 0
  832. $command_structuring_level{'majorheading'} = 1;
  833. $command_structuring_level{'centerchap'} = 1;
  834. our %sectioning_commands;
  835. foreach my $sectioning_command (keys (%command_structuring_level)) {
  836. $misc_commands{$sectioning_command} = 'line';
  837. if ($sectioning_command =~ /heading/) {
  838. $close_paragraph_commands{$sectioning_command} = 1;
  839. } else {
  840. $root_commands{$sectioning_command} = 1;
  841. }
  842. $sectioning_commands{$sectioning_command} = 1;
  843. }
  844. # misc commands that may be formatted as text.
  845. # index commands may be too, but index command may be added with
  846. # @def*index so they are not added here.
  847. my %formatted_misc_commands;
  848. foreach my $formatted_misc_command ('insertcopying', 'contents',
  849. 'shortcontents', 'summarycontents', 'center', 'printindex',
  850. 'listoffloats', 'shorttitlepage', 'settitle',
  851. 'author', 'subtitle', 'title', 'sp', 'exdent', 'headitem', 'item',
  852. 'itemx', 'tab', 'node', keys(%sectioning_commands)) {
  853. $formatted_misc_commands{$formatted_misc_command} = 1;
  854. }
  855. $root_commands{'node'} = 1;
  856. our %all_commands;
  857. foreach my $command (
  858. keys(%Texinfo::Common::block_commands),
  859. keys(%Texinfo::Common::brace_commands),
  860. keys(%Texinfo::Common::misc_commands),
  861. keys(%Texinfo::Common::no_brace_commands),
  862. qw(value),
  863. ) {
  864. $all_commands{$command} = 1;
  865. }
  866. our @MONTH_NAMES =
  867. (
  868. 'January', 'February', 'March', 'April', 'May',
  869. 'June', 'July', 'August', 'September', 'October',
  870. 'November', 'December'
  871. );
  872. sub locate_include_file($$)
  873. {
  874. my $self = shift;
  875. my $text = shift;
  876. my $file;
  877. my $ignore_include_directories = 0;
  878. my ($volume, $directories, $filename) = File::Spec->splitpath($text);
  879. my @directories = File::Spec->splitdir($directories);
  880. #print STDERR "$self $text @{$self->{'include_directories'}}\n";
  881. # If the path is absolute or begins with . or .., do not search in
  882. # include directories.
  883. if (File::Spec->file_name_is_absolute($text)) {
  884. $ignore_include_directories = 1;
  885. } else {
  886. foreach my $dir (@directories) {
  887. if ($dir eq File::Spec->updir() or $dir eq File::Spec->curdir()) {
  888. $ignore_include_directories = 1;
  889. last;
  890. } elsif ($dir ne '') {
  891. last;
  892. }
  893. }
  894. }
  895. #if ($text =~ m,^(/|\./|\.\./),) {
  896. if ($ignore_include_directories) {
  897. $file = $text if (-e $text and -r $text);
  898. } else {
  899. my @dirs;
  900. if ($self) {
  901. @dirs = @{$self->{'include_directories'}};
  902. } else {
  903. # no object with directory list and not an absolute path, never succeed
  904. return undef;
  905. }
  906. foreach my $include_dir (@{$self->{'include_directories'}}) {
  907. my ($include_volume, $include_directories, $include_filename)
  908. = File::Spec->splitpath($include_dir, 1);
  909. my $possible_file = File::Spec->catpath($include_volume,
  910. File::Spec->catdir(File::Spec->splitdir($include_directories),
  911. @directories), $filename);
  912. #$file = "$include_dir/$text" if (-e "$include_dir/$text" and -r "$include_dir/$text");
  913. $file = "$possible_file" if (-e "$possible_file" and -r "$possible_file");
  914. last if (defined($file));
  915. }
  916. }
  917. return $file;
  918. }
  919. sub open_out($$;$)
  920. {
  921. my $self = shift;
  922. my $file = shift;
  923. my $encoding = shift;
  924. if (!defined($encoding) and $self
  925. and defined($self->get_conf('OUTPUT_PERL_ENCODING'))) {
  926. $encoding = $self->get_conf('OUTPUT_PERL_ENCODING');
  927. }
  928. if ($file eq '-') {
  929. binmode(STDOUT, ":encoding($encoding)") if ($encoding);
  930. if ($self) {
  931. $self->{'unclosed_files'}->{$file} = \*STDOUT;
  932. }
  933. return \*STDOUT;
  934. }
  935. my $filehandle = do { local *FH };
  936. if (!open ($filehandle, '>', $file)) {
  937. return undef;
  938. }
  939. if ($encoding) {
  940. if ($encoding eq 'utf8' or $encoding eq 'utf-8-strict') {
  941. binmode($filehandle, ':utf8');
  942. } else { # FIXME also right for shiftijs or similar encodings?
  943. binmode($filehandle, ':bytes');
  944. }
  945. binmode($filehandle, ":encoding($encoding)");
  946. }
  947. if ($self) {
  948. push @{$self->{'opened_files'}}, $file;
  949. $self->{'unclosed_files'}->{$file} = $filehandle;
  950. #print STDERR "OOOOOOO $file ".join('|',@{$self->{'opened_files'}})."\n";
  951. #cluck;
  952. }
  953. return $filehandle;
  954. }
  955. sub warn_unknown_language($$) {
  956. my $lang = shift;
  957. my $gettext = shift;
  958. my @messages = ();
  959. my $lang_code = $lang;
  960. my $region_code;
  961. if ($lang =~ /^([a-z]+)_([A-Z]+)/) {
  962. $lang_code = $1;
  963. $region_code = $2;
  964. }
  965. if (! $Texinfo::Documentlanguages::language_codes{$lang_code}) {
  966. push @messages, sprintf(&$gettext(N__("%s is not a valid language code")),
  967. $lang_code);
  968. }
  969. if (defined($region_code)
  970. and ! $Texinfo::Documentlanguages::region_codes{$region_code}) {
  971. push @messages, sprintf(&$gettext(N__("%s is not a valid region code")),
  972. $region_code);
  973. }
  974. return @messages;
  975. }
  976. my %possible_split = (
  977. 'chapter' => 1,
  978. 'section' => 1,
  979. 'node' => 1,
  980. );
  981. sub warn_unknown_split($$) {
  982. my $split = shift;
  983. my $gettext = shift;
  984. my @messages = ();
  985. if ($split and !$possible_split{$split}) {
  986. push @messages, sprintf(&$gettext(N__("%s is not a valid split possibility")),
  987. $split);
  988. }
  989. return @messages;
  990. }
  991. # This should do the job, or at least don't do wrong if $self
  992. # is not defined, as could be the case if called from
  993. # Texinfo::Convert::Text.
  994. sub expand_verbatiminclude($$)
  995. {
  996. my $self = shift;
  997. my $current = shift;
  998. return unless ($current->{'extra'} and defined($current->{'extra'}->{'text_arg'}));
  999. my $text = $current->{'extra'}->{'text_arg'};
  1000. my $file = locate_include_file($self, $text);
  1001. my $verbatiminclude;
  1002. if (defined($file)) {
  1003. if (!open(VERBINCLUDE, $file)) {
  1004. if ($self) {
  1005. $self->line_error(sprintf($self->__("could not read %s: %s"), $file, $!),
  1006. $current->{'line_nr'});
  1007. }
  1008. } else {
  1009. if ($self and defined($self->get_conf('INPUT_PERL_ENCODING'))) {
  1010. binmode(VERBINCLUDE, ":encoding(".
  1011. $self->get_conf('INPUT_PERL_ENCODING').")");
  1012. }
  1013. $verbatiminclude = { 'cmdname' => 'verbatim',
  1014. 'parent' => $current->{'parent'},
  1015. 'extra' =>
  1016. {'text_arg' => $current->{'extra'}->{'text_arg'}} };
  1017. while (<VERBINCLUDE>) {
  1018. push @{$verbatiminclude->{'contents'}},
  1019. {'type' => 'raw', 'text' => $_ };
  1020. }
  1021. if (!close (VERBINCLUDE)) {
  1022. if ($self) {
  1023. $self->document_warn(sprintf($self->__(
  1024. "error on closing \@verbatiminclude file %s: %s"),
  1025. $file, $!));
  1026. }
  1027. }
  1028. }
  1029. } elsif ($self) {
  1030. $self->line_error(sprintf($self->__("\@%s: could not find %s"),
  1031. $current->{'cmdname'}, $text), $current->{'line_nr'});
  1032. }
  1033. return $verbatiminclude;
  1034. }
  1035. sub definition_category($$)
  1036. {
  1037. my $self = shift;
  1038. my $current = shift;
  1039. return undef if (!$current->{'extra'} or !$current->{'extra'}->{'def_args'});
  1040. my $arg_category = $current->{'extra'}->{'def_parsed_hash'}->{'category'};
  1041. my $arg_class = $current->{'extra'}->{'def_parsed_hash'}->{'class'};
  1042. return $arg_category
  1043. if (!defined($arg_class));
  1044. my $style = $command_index{$current->{'extra'}->{'def_command'}};
  1045. if ($style eq 'fn') {
  1046. if ($self) {
  1047. return $self->gdt('{category} on {class}', { 'category' => $arg_category,
  1048. 'class' => $arg_class });
  1049. } else {
  1050. return {'contents' => [$arg_category, {'text' => ' on '}, $arg_class]};
  1051. }
  1052. } elsif ($style eq 'vr') {
  1053. if ($self) {
  1054. return $self->gdt('{category} of {class}', { 'category' => $arg_category,
  1055. 'class' => $arg_class });
  1056. } else {
  1057. return {'contents' => [$arg_category, {'text' => ' of '}, $arg_class]};
  1058. }
  1059. }
  1060. }
  1061. sub expand_today($)
  1062. {
  1063. my $self = shift;
  1064. if ($self->get_conf('TEST')) {
  1065. return {'text' => 'a sunny day'};
  1066. }
  1067. my($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst)
  1068. = localtime(time);
  1069. $year += ($year < 70) ? 2000 : 1900;
  1070. return $self->gdt('{month} {day}, {year}',
  1071. { 'month' => $self->gdt($MONTH_NAMES[$mon]),
  1072. 'day' => $mday, 'year' => $year });
  1073. }
  1074. sub translated_command_tree($$)
  1075. {
  1076. my $self = shift;
  1077. my $cmdname = shift;
  1078. if ($self->{'translated_commands'}->{$cmdname}) {
  1079. return $self->gdt($self->{'translated_commands'}->{$cmdname});
  1080. }
  1081. return undef;
  1082. }
  1083. sub numbered_heading($$$;$)
  1084. {
  1085. my $self = shift;
  1086. my $current = shift;
  1087. my $text = shift;
  1088. my $numbered = shift;
  1089. my $number;
  1090. if (defined($current->{'number'}) and ($numbered or !defined($numbered))) {
  1091. $number = $current->{'number'};
  1092. }
  1093. my $result;
  1094. if ($self) {
  1095. if (defined($number)) {
  1096. if ($current->{'cmdname'} eq 'appendix' and $current->{'level'} == 1) {
  1097. $result = $self->gdt('Appendix {number} {section_title}',
  1098. {'number' => $number, 'section_title' => $text},
  1099. 'translated_text');
  1100. } else {
  1101. $result = $self->gdt('{number} {section_title}',
  1102. {'number' => $number, 'section_title' => $text},
  1103. 'translated_text');
  1104. }
  1105. } else {
  1106. $result = $text;
  1107. }
  1108. } else {
  1109. $result = $text;
  1110. $result = $number.' '.$result if (defined($number));
  1111. if ($current->{'cmdname'} eq 'appendix' and $current->{'level'} == 1) {
  1112. $result = 'Appendix '.$result;
  1113. }
  1114. }
  1115. chomp ($result);
  1116. return $result;
  1117. }
  1118. sub definition_arguments_content($)
  1119. {
  1120. my $root = shift;
  1121. my $result;
  1122. return undef if (!defined($root->{'extra'})
  1123. or !defined($root->{'extra'}->{'def_args'}));
  1124. my @args = @{$root->{'extra'}->{'def_args'}};
  1125. while (@args) {
  1126. last if ($args[0]->[0] ne 'spaces'
  1127. and !$root->{'extra'}->{'def_parsed_hash'}->{$args[0]->[0]});
  1128. shift @args;
  1129. }
  1130. if (@args) {
  1131. foreach my $arg (@args) {
  1132. push @$result, $arg->[1];
  1133. }
  1134. }
  1135. return $result;
  1136. }
  1137. # find the accent commands stack and the innermost text contents
  1138. sub find_innermost_accent_contents($;$)
  1139. {
  1140. my $current = shift;
  1141. my $encoding = shift;
  1142. my @accent_commands = ();
  1143. my $debug = 0;
  1144. ACCENT:
  1145. while (1) {
  1146. # the following can happen if called with a bad tree
  1147. if (!$current->{'cmdname'}
  1148. or !$accent_commands{$current->{'cmdname'}}) {
  1149. #print STDERR "BUG: Not an accent command in accent\n";
  1150. cluck "BUG: Not an accent command in accent\n";
  1151. #print STDERR Texinfo::Convert::Texinfo::convert($current)."\n";
  1152. #print STDERR Data::Dumper->Dump([$current]);
  1153. last;
  1154. }
  1155. push @accent_commands, $current;
  1156. # A bogus accent, that may happen
  1157. if (!$current->{'args'}) {
  1158. return ([], \@accent_commands);
  1159. }
  1160. my $arg = $current->{'args'}->[0];
  1161. if (!$arg->{'contents'}) {
  1162. print STDERR "BUG: No content in accent command\n";
  1163. #print STDERR Data::Dumper->Dump([$current]);
  1164. #print STDERR Texinfo::Convert::Texinfo::convert($current)."\n";
  1165. return ([], \@accent_commands);
  1166. }
  1167. # inside the argument of an accent
  1168. my $text_contents = [];
  1169. foreach my $content (@{$arg->{'contents'}}) {
  1170. if (!($content->{'extra'} and $content->{'extra'}->{'invalid_nesting'})
  1171. and !($content->{'cmdname'} and ($content->{'cmdname'} eq 'c'
  1172. or $content->{'cmdname'} eq 'comment'))) {
  1173. if ($content->{'cmdname'} and $accent_commands{$content->{'cmdname'}}) {
  1174. $current = $content;
  1175. next ACCENT;
  1176. } else {
  1177. push @$text_contents, $content;
  1178. }
  1179. }
  1180. }
  1181. # we go here if there was no nested accent
  1182. return ($text_contents, \@accent_commands);
  1183. }
  1184. }
  1185. sub trim_spaces_comment_from_content($)
  1186. {
  1187. my $contents = shift;
  1188. shift @$contents
  1189. if ($contents->[0] and $contents->[0]->{'type'}
  1190. and ($contents->[0]->{'type'} eq 'empty_line_after_command'
  1191. or $contents->[0]->{'type'} eq 'empty_spaces_after_command'
  1192. or $contents->[0]->{'type'} eq 'empty_spaces_before_argument'
  1193. or $contents->[0]->{'type'} eq 'empty_space_at_end_def_bracketed'
  1194. or $contents->[0]->{'type'} eq 'empty_spaces_after_close_brace'));
  1195. while (@$contents
  1196. and (($contents->[-1]->{'cmdname'}
  1197. and ($contents->[-1]->{'cmdname'} eq 'c'
  1198. or $contents->[-1]->{'cmdname'} eq 'comment'))
  1199. or ($contents->[-1]->{'type'}
  1200. and ($contents->[-1]->{'type'} eq 'spaces_at_end'
  1201. or $contents->[-1]->{'type'} eq 'space_at_end_block_command')))) {
  1202. pop @$contents;
  1203. }
  1204. }
  1205. sub float_name_caption($$)
  1206. {
  1207. my $self = shift;
  1208. my $root = shift;
  1209. my $caption;
  1210. if ($root->{'extra'}->{'caption'}) {
  1211. $caption = $root->{'extra'}->{'caption'};
  1212. } elsif ($root->{'extra'}->{'shortcaption'}) {
  1213. $caption = $root->{'extra'}->{'shortcaption'};
  1214. }
  1215. #if ($self->get_conf('DEBUG')) {
  1216. # my $caption_texi =
  1217. # Texinfo::Convert::Texinfo::convert({ 'contents' => $caption->{'contents'}});
  1218. # print STDERR " CAPTION: $caption_texi\n";
  1219. #}
  1220. my $type;
  1221. if ($root->{'extra'}->{'type'}->{'normalized'} ne '') {
  1222. $type = {'contents' => $root->{'extra'}->{'type'}->{'content'}};
  1223. }
  1224. my $prepended;
  1225. if ($type) {
  1226. if ($caption) {
  1227. if (defined($root->{'number'})) {
  1228. $prepended = $self->gdt('{float_type} {float_number}: ',
  1229. {'float_type' => $type,
  1230. 'float_number' => $root->{'number'}});
  1231. } else {
  1232. $prepended = $self->gdt('{float_type}: ',
  1233. {'float_type' => $type});
  1234. }
  1235. } else {
  1236. if (defined($root->{'number'})) {
  1237. $prepended = $self->gdt("{float_type} {float_number}\n",
  1238. {'float_type' => $type,
  1239. 'float_number' => $root->{'number'}});
  1240. } else {
  1241. $prepended = $self->gdt("{float_type}\n",
  1242. {'float_type' => $type});
  1243. }
  1244. }
  1245. } elsif (defined($root->{'number'})) {
  1246. if ($caption) {
  1247. $prepended = $self->gdt('{float_number}: ',
  1248. {'float_number' => $root->{'number'}});
  1249. } else {
  1250. $prepended = $self->gdt("{float_number}\n",
  1251. {'float_number' => $root->{'number'}});
  1252. }
  1253. }
  1254. return ($caption, $prepended);
  1255. }
  1256. # decompose a decimal number on a given base.
  1257. sub _decompose_integer($$)
  1258. {
  1259. my $number = shift;
  1260. my $base = shift;
  1261. my @result = ();
  1262. while ($number >= 0) {
  1263. my $factor = $number % $base;
  1264. push (@result, $factor);
  1265. $number = int(($number - $factor) / $base) - 1;
  1266. }
  1267. return @result;
  1268. }
  1269. sub enumerate_item_representation($$)
  1270. {
  1271. my $specification = shift;
  1272. my $number = shift;
  1273. if ($specification =~ /^[0-9]+$/) {
  1274. return $specification + $number -1;
  1275. }
  1276. my $result = '';
  1277. my $base_letter = ord('a');
  1278. $base_letter = ord('A') if (ucfirst($specification) eq $specification);
  1279. my @letter_ords = _decompose_integer(ord($specification) - $base_letter + $number - 1, 26);
  1280. foreach my $ord (@letter_ords) {
  1281. $result = chr($base_letter + $ord) . $result;
  1282. }
  1283. return $result;
  1284. }
  1285. sub is_content_empty($;$);
  1286. sub is_content_empty($;$)
  1287. {
  1288. my $tree = shift;
  1289. my $do_not_ignore_index_entries = shift;
  1290. if (!defined($tree) or !exists($tree->{'contents'})) {
  1291. return 1;
  1292. }
  1293. foreach my $content (@{$tree->{'contents'}}) {
  1294. #print STDERR _print_current($content);
  1295. if ($content->{'cmdname'}) {
  1296. if ($content->{'type'} and $content->{'type'} eq 'index_entry_command') {
  1297. if ($do_not_ignore_index_entries) {
  1298. return 0;
  1299. } else {
  1300. next;
  1301. }
  1302. }
  1303. if (exists($misc_commands{$content->{'cmdname'}})) {
  1304. my @truc = keys(%formatted_misc_commands);
  1305. if ($formatted_misc_commands{$content->{'cmdname'}}) {
  1306. return 0;
  1307. } else {
  1308. next;
  1309. }
  1310. } elsif ($unformatted_brace_commands{$content->{'cmdname'}}
  1311. or $unformatted_block_commands{$content->{'cmdname'}}) {
  1312. next;
  1313. } else {
  1314. return 0;
  1315. }
  1316. }
  1317. if ($content->{'type'}) {
  1318. if ($content->{'type'} eq 'paragraph') {
  1319. return 0;
  1320. }
  1321. }
  1322. if ($content->{'text'} and $content->{'text'} =~ /\S/) {
  1323. return 0;
  1324. }
  1325. if (not is_content_empty($content, $do_not_ignore_index_entries)) {
  1326. return 0;
  1327. }
  1328. }
  1329. return 1;
  1330. }
  1331. our %htmlxref_entries = (
  1332. 'node' => [ 'node', 'section', 'chapter', 'mono' ],
  1333. 'section' => [ 'section', 'chapter','node', 'mono' ],
  1334. 'chapter' => [ 'chapter', 'section', 'node', 'mono' ],
  1335. 'mono' => [ 'mono', 'chapter', 'section', 'node' ],
  1336. );
  1337. sub parse_htmlxref_files($$)
  1338. {
  1339. my $self = shift;
  1340. my $files = shift;
  1341. my $htmlxref;
  1342. foreach my $file (@$files) {
  1343. print STDERR "html refs config file: $file\n" if ($self->get_conf('DEBUG'));
  1344. unless (open (HTMLXREF, $file)) {
  1345. $self->document_warn(
  1346. sprintf($self->__("could not open html refs config file %s: %s"),
  1347. $file, $!));
  1348. next;
  1349. }
  1350. my $line_nr = 0;
  1351. my %variables;
  1352. while (my $hline = <HTMLXREF>) {
  1353. my $line = $hline;
  1354. $line_nr++;
  1355. next if $hline =~ /^\s*#/;
  1356. #$hline =~ s/[#]\s.*//;
  1357. $hline =~ s/^\s*//;
  1358. next if $hline =~ /^\s*$/;
  1359. chomp ($hline);
  1360. if ($hline =~ s/^\s*(\w+)\s*=\s*//) {
  1361. # handle variables
  1362. my $var = $1;
  1363. my $re = join '|', map { quotemeta $_ } keys %variables;
  1364. $hline =~ s/\$\{($re)\}/defined $variables{$1} ? $variables{$1}
  1365. : "\${$1}"/ge;
  1366. $variables{$var} = $hline;
  1367. next;
  1368. }
  1369. my @htmlxref = split /\s+/, $hline;
  1370. my $manual = shift @htmlxref;
  1371. my $split_or_mono = shift @htmlxref;
  1372. #print STDERR "$split_or_mono $Texi2HTML::Config::htmlxref_entries{$split_or_mono} $line_nr\n";
  1373. if (!defined($split_or_mono)) {
  1374. $self->file_line_warn($self->__("missing type"), $file, $line_nr);
  1375. next;
  1376. } elsif (!defined($htmlxref_entries{$split_or_mono})) {
  1377. $self->file_line_warn(sprintf($self->__("unrecognized type: %s"),
  1378. $split_or_mono), $file, $line_nr);
  1379. next;
  1380. }
  1381. my $href = shift @htmlxref;
  1382. next if (exists($htmlxref->{$manual}->{$split_or_mono}));
  1383. if (defined($href)) { # substitute 'variables'
  1384. my $re = join '|', map { quotemeta $_ } keys %variables;
  1385. $href =~ s/\$\{($re)\}/defined $variables{$1} ? $variables{$1}
  1386. : "\${$1}"/ge;
  1387. $href =~ s/\/*$// if ($split_or_mono ne 'mono');
  1388. }
  1389. $htmlxref->{$manual}->{$split_or_mono} = $href;
  1390. }
  1391. if (!close (HTMLXREF)) {
  1392. $self->document_warn(sprintf($self->__(
  1393. "error on closing html refs config file %s: %s"),
  1394. $file, $!));
  1395. }
  1396. }
  1397. return $htmlxref;
  1398. }
  1399. sub parse_renamed_nodes_file($$;$$)
  1400. {
  1401. my $self = shift;
  1402. my $renamed_nodes_file = shift;
  1403. # if not given they are automatically created
  1404. my $renamed_nodes = shift;
  1405. my $renamed_nodes_lines = shift;
  1406. if (open(RENAMEDFILE, "<$renamed_nodes_file")) {
  1407. if ($self->get_conf('INPUT_PERL_ENCODING')) {
  1408. binmode(RENAMEDFILE, ":encoding(".
  1409. $self->get_conf('INPUT_PERL_ENCODING').")");
  1410. }
  1411. my $renamed_nodes_line_nr = 0;
  1412. my @old_names = ();
  1413. while (<RENAMEDFILE>) {
  1414. $renamed_nodes_line_nr++;
  1415. next unless (/\S/);
  1416. next if (/^\s*\@c\b/);
  1417. if (s/^\s*\@\@\{\}\s+(\S)/$1/) {
  1418. chomp;
  1419. if (scalar(@old_names)) {
  1420. foreach my $old_node_name (@old_names) {
  1421. $renamed_nodes->{$old_node_name} = $_;
  1422. }
  1423. $renamed_nodes_lines->{$_} = $renamed_nodes_line_nr;
  1424. @old_names = ();
  1425. } else {
  1426. $self->file_line_warn($self->__("no node to be renamed"),
  1427. $renamed_nodes_file, $renamed_nodes_line_nr);
  1428. }
  1429. } else {
  1430. chomp;
  1431. s/^\s*//;
  1432. $renamed_nodes_lines->{$_} = $renamed_nodes_line_nr;
  1433. push @old_names, $_;
  1434. }
  1435. }
  1436. if (scalar(@old_names)) {
  1437. $self->file_line_warn($self->__("nodes without a new name at the end of file"),
  1438. $renamed_nodes_file, $renamed_nodes_line_nr);
  1439. }
  1440. if (!close(RENAMEDFILE)) {
  1441. $self->document_warn(sprintf($self->__p(
  1442. "see HTML Xref Link Preservation in the Texinfo manual for context",
  1443. "error on closing node-renaming configuration file %s: %s"),
  1444. $renamed_nodes_file, $!));
  1445. }
  1446. } else {
  1447. $self->document_warn(sprintf($self->__("could not open %s: %s"),
  1448. $renamed_nodes_file, $!));
  1449. }
  1450. return ($renamed_nodes, $renamed_nodes_lines);
  1451. }
  1452. sub collect_renamed_nodes($$;$$)
  1453. {
  1454. my $self = shift;
  1455. my $basename = shift;
  1456. my $renamed_nodes = shift;
  1457. my $renamed_nodes_lines = shift;
  1458. my $renamed_nodes_file;
  1459. if (defined($self->get_conf('RENAMED_NODES_FILE'))) {
  1460. $renamed_nodes_file = $self->get_conf('RENAMED_NODES_FILE');
  1461. } elsif (-f $basename . '-noderename.cnf') {
  1462. $renamed_nodes_file = $basename . '-noderename.cnf';
  1463. }
  1464. if (defined($renamed_nodes_file)) {
  1465. my ($renamed_nodes, $renamed_nodes_lines)
  1466. = parse_renamed_nodes_file($self, $renamed_nodes_file, $renamed_nodes,
  1467. $renamed_nodes_lines);
  1468. return ($renamed_nodes, $renamed_nodes_lines, $renamed_nodes_file);
  1469. }
  1470. return (undef, undef, undef);
  1471. }
  1472. sub normalize_top_node_name($)
  1473. {
  1474. my $node = shift;
  1475. if ($node =~ /^top$/i) {
  1476. return 'Top';
  1477. }
  1478. return $node;
  1479. }
  1480. sub _convert_text_options($)
  1481. {
  1482. my $self = shift;
  1483. my %options;
  1484. if ($self->get_conf('ENABLE_ENCODING')) {
  1485. if ($self->get_conf('OUTPUT_ENCODING_NAME')) {
  1486. $options{'enabled_encoding'} = $self->get_conf('OUTPUT_ENCODING_NAME');
  1487. } elsif ($self->get_conf('INPUT_ENCODING_NAME')) {
  1488. $options{'enabled_encoding'} = $self->get_conf('INPUT_ENCODING_NAME');
  1489. }
  1490. }
  1491. $options{'TEST'} = 1 if ($self->get_conf('TEST'));
  1492. $options{'NUMBER_SECTIONS'} = $self->get_conf('NUMBER_SECTIONS');
  1493. $options{'converter'} = $self;
  1494. $options{'expanded_formats_hash'} = $self->{'expanded_formats_hash'};
  1495. return %options;
  1496. }
  1497. sub count_bytes($$;$)
  1498. {
  1499. my $self = shift;
  1500. my $string = shift;
  1501. my $encoding = shift;
  1502. if (!defined($encoding) and $self and $self->get_conf('OUTPUT_PERL_ENCODING')) {
  1503. $encoding = $self->get_conf('OUTPUT_PERL_ENCODING');
  1504. }
  1505. if ($encoding and $encoding ne 'ascii') {
  1506. return length(Encode::encode($encoding, $string));
  1507. } else {
  1508. return length($string);
  1509. #my $length = length($string);
  1510. #$string =~ s/\n/\\n/g;
  1511. #$string =~ s/\f/\\f/g;
  1512. #print STDERR "Count($length): $string\n";
  1513. #return $length;
  1514. }
  1515. # FIXME is the following required for correct count of end of lines?
  1516. #if ($encoding) {
  1517. # return length(Encode::encode($encoding, $string));
  1518. #} else {
  1519. # return length(Encode::encode('ascii', $string));
  1520. #}
  1521. }
  1522. # TODO
  1523. # also recurse into
  1524. # extra->misc_args, extra->args_index
  1525. # extra->index_entry extra->type
  1526. #
  1527. # extra that should point to other elements:
  1528. # command_as_argument
  1529. # @block_command_line_contents @brace_command_contents @misc_content end_command
  1530. # associated_section part_associated_section associated_node associated_part
  1531. # @prototypes @columnfractions titlepage quotation @author command
  1532. # menu_entry_description menu_entry_name
  1533. #
  1534. # should point to other elements, or be copied. And some should be recursed
  1535. # into too.
  1536. # extra->type->content
  1537. # extra->nodes_manuals->[]
  1538. # extra->node_content
  1539. # extra->node_argument
  1540. # extra->explanation_contents
  1541. # extra->menu_entry_node
  1542. # extra->def_arg
  1543. sub _copy_tree($$$);
  1544. sub _copy_tree($$$)
  1545. {
  1546. my $current = shift;
  1547. my $parent = shift;
  1548. my $reference_associations = shift;
  1549. my $new = {};
  1550. $reference_associations->{$current} = $new;
  1551. $new->{'parent'} = $parent if ($parent);
  1552. foreach my $key ('type', 'cmdname', 'text') {
  1553. $new->{$key} = $current->{$key} if (exists($current->{$key}));
  1554. }
  1555. foreach my $key ('args', 'contents') {
  1556. if ($current->{$key}) {
  1557. if (ref($current->{$key}) ne 'ARRAY') {
  1558. my $command_or_type = '';
  1559. if ($new->{'cmdname'}) {
  1560. $command_or_type = '@'.$new->{'cmdname'};
  1561. } elsif ($new->{'type'}) {
  1562. $command_or_type = $new->{'type'};
  1563. }
  1564. print STDERR "Not an array [$command_or_type] $key ".ref($current->{$key})."\n";
  1565. }
  1566. $new->{$key} = [];
  1567. $reference_associations->{$current->{$key}} = $new->{$key};
  1568. foreach my $child (@{$current->{$key}}) {
  1569. push @{$new->{$key}}, _copy_tree($child, $new, $reference_associations);
  1570. }
  1571. }
  1572. }
  1573. if ($current->{'extra'}) {
  1574. $new->{'extra'} = {};
  1575. foreach my $key (keys %{$current->{'extra'}}) {
  1576. if ($current->{'cmdname'} and $current->{'cmdname'} eq 'multitable'
  1577. and $key eq 'prototypes') {
  1578. $new->{'extra'}->{$key} = [];
  1579. $reference_associations->{$current->{'extra'}->{$key}} = $new->{$key};
  1580. foreach my $child (@{$current->{'extra'}->{$key}}) {
  1581. push @{$new->{'extra'}->{$key}},
  1582. _copy_tree($child, $new, $reference_associations);
  1583. }
  1584. } elsif (!ref($current->{'extra'}->{$key})) {
  1585. $new->{'extra'}->{$key} = $current->{'extra'}->{$key};
  1586. }
  1587. }
  1588. }
  1589. return $new;
  1590. }
  1591. # Not used.
  1592. sub _collect_references($$);
  1593. sub _collect_references($$)
  1594. {
  1595. my $current = shift;
  1596. my $references = shift;
  1597. foreach my $key ('args', 'contents') {
  1598. if ($current->{$key}) {
  1599. $references->{$current->{$key}} = $current->{$key};
  1600. foreach my $child (@{$current->{$key}}) {
  1601. $references->{$child} = $child;
  1602. _collect_references($child, $references);
  1603. }
  1604. }
  1605. }
  1606. }
  1607. sub _substitute_references_in_array($$$);
  1608. sub _substitute_references_in_array($$$)
  1609. {
  1610. my $array = shift;
  1611. my $reference_associations = shift;
  1612. my $context = shift;
  1613. my $result = [];
  1614. my $index = 0;
  1615. foreach my $item (@{$array}) {
  1616. if (!ref($item)) {
  1617. push @{$result}, $item;
  1618. } elsif ($reference_associations->{$item}) {
  1619. push @{$result}, $reference_associations->{$item};
  1620. } elsif (ref($item) eq 'ARRAY') {
  1621. push @$result,
  1622. _substitute_references_in_array($item, $reference_associations,
  1623. "$context [$index]");
  1624. } elsif (defined($item->{'text'})) {
  1625. my $new_text = _copy_tree($item, undef, $reference_associations);
  1626. substitute_references($item, $new_text, $reference_associations);
  1627. push @{$result}, $new_text;
  1628. } else {
  1629. print STDERR "Trouble with $context [$index] (".ref($item).")\n";
  1630. push @{$result}, undef;
  1631. }
  1632. $index++;
  1633. }
  1634. return $result;
  1635. }
  1636. sub substitute_references($$$);
  1637. sub substitute_references($$$)
  1638. {
  1639. my $current = shift;
  1640. my $new = shift;
  1641. my $reference_associations = shift;
  1642. foreach my $key ('args', 'contents') {
  1643. if ($new->{$key}) {
  1644. my $index = 0;
  1645. foreach my $child (@{$new->{$key}}) {
  1646. substitute_references($child, $current->{$key}->[$index],
  1647. $reference_associations);
  1648. $index++;
  1649. }
  1650. }
  1651. }
  1652. if ($current->{'extra'}) {
  1653. foreach my $key (keys %{$current->{'extra'}}) {
  1654. if (ref($current->{'extra'}->{$key})) {
  1655. my $command_or_type = '';
  1656. if ($new->{'cmdname'}) {
  1657. $command_or_type = '@'.$new->{'cmdname'};
  1658. } elsif ($new->{'type'}) {
  1659. $command_or_type = $new->{'type'};
  1660. }
  1661. if ($current->{'cmdname'} and $current->{'cmdname'} eq 'multitable'
  1662. and $key eq 'prototypes') {
  1663. my $index = 0;
  1664. foreach my $child (@{$new->{'extra'}->{$key}}) {
  1665. substitute_references($child, $current->{'extra'}->{$key}->[$index],
  1666. $reference_associations);
  1667. $index++;
  1668. }
  1669. } elsif ($reference_associations->{$current->{'extra'}->{$key}}) {
  1670. $new->{'extra'}->{$key}
  1671. = $reference_associations->{$current->{'extra'}->{$key}};
  1672. #print STDERR "Done [$command_or_type]: $key\n";
  1673. } else {
  1674. if (ref($current->{'extra'}->{$key}) eq 'ARRAY') {
  1675. #print STDERR "Array $command_or_type -> $key\n";
  1676. $new->{'extra'}->{$key} = _substitute_references_in_array(
  1677. $current->{'extra'}->{$key}, $reference_associations,
  1678. "[$command_or_type]{$key}");
  1679. } else {
  1680. if (($current->{'cmdname'}
  1681. and ($current->{'cmdname'} eq 'listoffloats'
  1682. or $current->{'cmdname'} eq 'float')
  1683. and $key eq 'type')
  1684. or ($key eq 'index_entry')
  1685. or ($current->{'type'}
  1686. and $current->{'type'} eq 'menu_entry'
  1687. and $key eq 'menu_entry_node')) {
  1688. foreach my $type_key (keys(%{$current->{'extra'}->{$key}})) {
  1689. if (!ref($current->{'extra'}->{$key}->{$type_key})) {
  1690. $new->{'extra'}->{$key}->{$type_key}
  1691. = $current->{'extra'}->{$key}->{$type_key};
  1692. } elsif ($reference_associations->{$current->{'extra'}->{$key}->{$type_key}}) {
  1693. $new->{'extra'}->{$key}->{$type_key}
  1694. = $reference_associations->{$current->{'extra'}->{$key}->{$type_key}};
  1695. } elsif (ref($current->{'extra'}->{$key}->{$type_key}) eq 'ARRAY') {
  1696. $new->{'extra'}->{$key}->{$type_key}
  1697. = _substitute_references_in_array(
  1698. $current->{'extra'}->{$key}->{$type_key},
  1699. $reference_associations,
  1700. "[$command_or_type]{$key}{$type_key}");
  1701. } else {
  1702. print STDERR "Not substituting [$command_or_type]{$key}: $type_key\n";
  1703. }
  1704. }
  1705. } else {
  1706. print STDERR "Not substituting [$command_or_type]: $key ($current->{'extra'}->{$key})\n";
  1707. }
  1708. }
  1709. }
  1710. }
  1711. }
  1712. }
  1713. }
  1714. sub copy_tree($;$)
  1715. {
  1716. my $current = shift;
  1717. my $parent = shift;
  1718. my $reference_associations = {};
  1719. my $copy = _copy_tree($current, $parent, $reference_associations);
  1720. substitute_references($current, $copy, $reference_associations);
  1721. return $copy;
  1722. }
  1723. sub modify_tree($$$;$);
  1724. sub modify_tree($$$;$)
  1725. {
  1726. my $self = shift;
  1727. my $tree = shift;
  1728. my $operation = shift;
  1729. my $argument = shift;
  1730. #print STDERR "modify_tree tree: $tree\n";
  1731. if ($tree->{'args'}) {
  1732. my @args = @{$tree->{'args'}};
  1733. for (my $i = 0; $i <= $#args; $i++) {
  1734. my @new_args = &$operation($self, 'arg', $args[$i], $argument);
  1735. modify_tree($self, $args[$i], $operation, $argument);
  1736. # this puts the new args at the place of the old arg using the
  1737. # offset from the end of the array
  1738. splice (@{$tree->{'args'}}, $i - $#args -1, 1, @new_args);
  1739. #foreach my $arg (@new_args) {
  1740. # modify_tree($self, $arg, $operation);
  1741. #}
  1742. }
  1743. }
  1744. if ($tree->{'contents'}) {
  1745. my @contents = @{$tree->{'contents'}};
  1746. for (my $i = 0; $i <= $#contents; $i++) {
  1747. my @new_contents = &$operation($self, 'content', $contents[$i], $argument);
  1748. modify_tree($self, $contents[$i], $operation, $argument);
  1749. # this puts the new contents at the place of the old content using the
  1750. # offset from the end of the array
  1751. splice (@{$tree->{'contents'}}, $i - $#contents -1, 1, @new_contents);
  1752. #foreach my $content (@new_contents) {
  1753. # modify_tree($self, $content, $operation);
  1754. #}
  1755. }
  1756. }
  1757. return $tree;
  1758. }
  1759. sub _protect_comma($$$)
  1760. {
  1761. my $self = shift;
  1762. my $type = shift;
  1763. my $current = shift;
  1764. return _protect_text($current, quotemeta(','));
  1765. }
  1766. sub protect_comma_in_tree($)
  1767. {
  1768. my $tree = shift;
  1769. return modify_tree(undef, $tree, \&_protect_comma);
  1770. }
  1771. sub _new_asis_command_with_text($$;$)
  1772. {
  1773. my $text = shift;
  1774. my $parent = shift;
  1775. my $text_type = shift;
  1776. my $new_command = {'cmdname' => 'asis', 'parent' => $parent };
  1777. push @{$new_command->{'args'}}, {'type' => 'brace_command_arg',
  1778. 'parent' => $new_command};
  1779. push @{$new_command->{'args'}->[0]->{'contents'}}, {
  1780. 'text' => $text,
  1781. 'parent' => $new_command->{'args'}->[0]};
  1782. if (defined($text_type)) {
  1783. $new_command->{'args'}->[0]->{'contents'}->[0]->{'type'} = $text_type;
  1784. }
  1785. return $new_command;
  1786. }
  1787. sub _protect_text($$)
  1788. {
  1789. my $current = shift;
  1790. my $to_protect = shift;
  1791. #print STDERR "$to_protect: $current "._print_current($current)."\n";
  1792. if (defined($current->{'text'}) and $current->{'text'} =~ /$to_protect/
  1793. and !(defined($current->{'type'}) and $current->{'type'} eq 'raw')) {
  1794. my @result = ();
  1795. my $remaining_text = $current->{'text'};
  1796. while ($remaining_text) {
  1797. if ($remaining_text =~ s/^(.*?)(($to_protect)+)//) {
  1798. if ($1 ne '') {
  1799. push @result, {'text' => $1, 'parent' => $current->{'parent'}};
  1800. $result[-1]->{'type'} = $current->{'type'}
  1801. if defined($current->{'type'});
  1802. }
  1803. if ($to_protect eq quotemeta(',')) {
  1804. for (my $i = 0; $i < length($2); $i++) {
  1805. push @result, {'cmdname' => 'comma', 'parent' => $current->{'parent'},
  1806. 'args' => [{'type' => 'brace_command_arg'}]};
  1807. }
  1808. } else {
  1809. push @result, _new_asis_command_with_text($2, $current->{'parent'},
  1810. $current->{'type'});
  1811. }
  1812. } else {
  1813. push @result, {'text' => $remaining_text, 'parent' => $current->{'parent'}};
  1814. $result[-1]->{'type'} = $current->{'type'}
  1815. if defined($current->{'type'});
  1816. last;
  1817. }
  1818. }
  1819. #print STDERR "Result: @result\n";
  1820. return @result;
  1821. } else {
  1822. #print STDERR "No change: $current\n";
  1823. return ($current);
  1824. }
  1825. }
  1826. sub _protect_colon($$$)
  1827. {
  1828. my $self = shift;
  1829. my $type = shift;
  1830. my $current = shift;
  1831. return _protect_text ($current, quotemeta(':'));
  1832. }
  1833. sub protect_colon_in_tree($)
  1834. {
  1835. my $tree = shift;
  1836. return modify_tree(undef, $tree, \&_protect_colon);
  1837. }
  1838. sub _protect_node_after_label($$$)
  1839. {
  1840. my $self = shift;
  1841. my $type = shift;
  1842. my $current = shift;
  1843. return _protect_text ($current, '['. quotemeta(".\t,") .']');
  1844. }
  1845. sub protect_node_after_label_in_tree($)
  1846. {
  1847. my $tree = shift;
  1848. return modify_tree(undef, $tree, \&_protect_node_after_label);
  1849. }
  1850. sub _is_cpp_line($)
  1851. {
  1852. my $text = shift;
  1853. return 1 if ($text =~ /^\s*#\s*(line)? (\d+)(( "([^"]+)")(\s+\d+)*)?\s*$/);
  1854. return 0;
  1855. }
  1856. sub _protect_hashchar_at_line_beginning($$$)
  1857. {
  1858. my $self = shift;
  1859. my $type = shift;
  1860. my $current = shift;
  1861. #print STDERR "$type $current "._print_current($current)."\n";
  1862. # if the next is a hash character at line beginning, mark it
  1863. if (defined($current->{'text'}) and $current->{'text'} =~ /\n$/
  1864. and $current->{'parent'} and $current->{'parent'}->{'contents'}) {
  1865. my $parent = $current->{'parent'};
  1866. #print STDERR "End of line in $current, parent $parent: (@{$parent->{'contents'}})\n";
  1867. my $current_found = 0;
  1868. foreach my $content (@{$parent->{'contents'}}) {
  1869. if ($current_found) {
  1870. #print STDERR "after $current: $content $content->{'text'}\n";
  1871. if ($content->{'text'} and _is_cpp_line($content->{'text'})) {
  1872. $content->{'extra'}->{'_protect_hashchar'} = 1;
  1873. }
  1874. last;
  1875. } elsif ($content eq $current) {
  1876. $current_found = 1;
  1877. }
  1878. }
  1879. }
  1880. my $protect_hash = 0;
  1881. # if marked, or first and a cpp_line protect a leading hash character
  1882. if ($current->{'extra'} and $current->{'extra'}->{'_protect_hashchar'}) {
  1883. delete $current->{'extra'}->{'_protect_hashchar'};
  1884. if (!scalar(keys(%{$current->{'extra'}}))) {
  1885. delete $current->{'extra'};
  1886. }
  1887. $protect_hash = 1;
  1888. } elsif ($current->{'parent'} and $current->{'parent'}->{'contents'}
  1889. and $current->{'parent'}->{'contents'}->[0]
  1890. and $current->{'parent'}->{'contents'}->[0] eq $current
  1891. and $current->{'text'}
  1892. and _is_cpp_line($current->{'text'})) {
  1893. $protect_hash = 1;
  1894. }
  1895. if ($protect_hash) {
  1896. my @result = ();
  1897. if ($current->{'type'} and $current->{'type'} eq 'raw') {
  1898. if ($self) {
  1899. my $parent = $current->{'parent'};
  1900. while ($parent) {
  1901. if ($parent->{'cmdname'} and $parent->{'line_nr'}) {
  1902. $self->line_warn(sprintf($self->__(
  1903. "could not protect hash character in \@%s"),
  1904. $parent->{'cmdname'}), $parent->{'line_nr'});
  1905. last;
  1906. }
  1907. $parent = $parent->{'parent'};
  1908. }
  1909. }
  1910. } else {
  1911. $current->{'text'} =~ s/^(\s*)#//;
  1912. if ($1 ne '') {
  1913. push @result, {'text' => $1, 'parent' => $current->{'parent'}};
  1914. }
  1915. push @result, {'cmdname' => 'hashchar', 'parent' => $current->{'parent'},
  1916. 'args' => [{'type' => 'brace_command_arg'}]};
  1917. }
  1918. push @result, $current;
  1919. return @result;
  1920. } else {
  1921. return ($current);
  1922. }
  1923. }
  1924. sub protect_hashchar_at_line_beginning($$)
  1925. {
  1926. my $self = shift;
  1927. my $tree = shift;
  1928. return modify_tree($self, $tree, \&_protect_hashchar_at_line_beginning);
  1929. }
  1930. sub protect_first_parenthesis($)
  1931. {
  1932. my $contents = shift;
  1933. return undef if (!defined ($contents));
  1934. my @contents = @$contents;
  1935. my $brace;
  1936. if ($contents[0] and $contents->[0]{'text'} and $contents[0]->{'text'} =~ /^\(/) {
  1937. if ($contents[0]->{'text'} !~ /^\($/) {
  1938. $brace = shift @contents;
  1939. my $brace_text = $brace->{'text'};
  1940. $brace_text =~ s/^\(//;
  1941. unshift @contents, { 'text' => $brace_text, 'type' => $brace->{'type'},
  1942. 'parent' => $brace->{'parent'} } if $brace_text ne '';
  1943. } else {
  1944. $brace = shift @contents;
  1945. }
  1946. unshift @contents, _new_asis_command_with_text('(', $brace->{'parent'},
  1947. $brace->{'type'});
  1948. }
  1949. return \@contents;
  1950. }
  1951. sub find_parent_root_command($$)
  1952. {
  1953. my $parser = shift;
  1954. my $current = shift;
  1955. my $root_command;
  1956. while (1) {
  1957. if ($current->{'cmdname'}) {
  1958. if ($root_commands{$current->{'cmdname'}}) {
  1959. return $current;
  1960. } elsif ($region_commands{$current->{'cmdname'}}) {
  1961. if ($current->{'cmdname'} eq 'copying' and $parser
  1962. and $parser->{'extra'} and $parser->{'extra'}->{'insertcopying'}) {
  1963. foreach my $insertcopying(@{$parser->{'extra'}->{'insertcopying'}}) {
  1964. my $root_command
  1965. = $parser->find_parent_root_command($insertcopying);
  1966. return $root_command if (defined($root_command));
  1967. }
  1968. } else {
  1969. return undef;
  1970. }
  1971. }
  1972. }
  1973. if ($current->{'parent'}) {
  1974. $current = $current->{'parent'};
  1975. } else {
  1976. return undef;
  1977. }
  1978. }
  1979. # Should never get there
  1980. return undef;
  1981. }
  1982. # for debugging
  1983. sub _print_current($)
  1984. {
  1985. my $current = shift;
  1986. if (ref($current) ne 'HASH') {
  1987. return "_print_current: $current not a hash\n";
  1988. }
  1989. my $type = '';
  1990. my $cmd = '';
  1991. my $parent_string = '';
  1992. my $text = '';
  1993. $type = "($current->{'type'})" if (defined($current->{'type'}));
  1994. $cmd = "\@$current->{'cmdname'}" if (defined($current->{'cmdname'}));
  1995. $cmd .= "($current->{'level'})" if (defined($current->{'level'}));
  1996. $text = "[text: $current->{'text'}]" if (defined($current->{'text'}));
  1997. if ($current->{'parent'}) {
  1998. my $parent = $current->{'parent'};
  1999. my $parent_cmd = '';
  2000. my $parent_type = '';
  2001. $parent_cmd = "\@$parent->{'cmdname'}" if (defined($parent->{'cmdname'}));
  2002. $parent_type = "($parent->{'type'})" if (defined($parent->{'type'}));
  2003. $parent_string = " <- $parent_cmd$parent_type\n";
  2004. }
  2005. my $args = '';
  2006. my $contents = '';
  2007. $args = "args(".scalar(@{$current->{'args'}}).')' if $current->{'args'};
  2008. $contents = "contents(".scalar(@{$current->{'contents'}}).')'
  2009. if $current->{'contents'};
  2010. if ("$cmd$type" ne '') {
  2011. return "$cmd$type : $text $args $contents\n$parent_string";
  2012. } else {
  2013. return "$text $args $contents\n$parent_string";
  2014. }
  2015. }
  2016. sub move_index_entries_after_items($) {
  2017. # enumerate or itemize
  2018. my $current = shift;
  2019. return unless ($current->{'contents'});
  2020. my $previous;
  2021. foreach my $item (@{$current->{'contents'}}) {
  2022. #print STDERR "Before proceeding: $previous $item->{'cmdname'} (@{$previous->{'contents'}})\n" if ($previous and $previous->{'contents'});
  2023. if (defined($previous) and $item->{'cmdname'}
  2024. and $item->{'cmdname'} eq 'item'
  2025. and $previous->{'contents'} and scalar(@{$previous->{'contents'}})) {
  2026. my $previous_ending_container;
  2027. if ($previous->{'contents'}->[-1]->{'type'}
  2028. and ($previous->{'contents'}->[-1]->{'type'} eq 'paragraph'
  2029. or $previous->{'contents'}->[-1]->{'type'} eq 'preformatted')) {
  2030. $previous_ending_container = $previous->{'contents'}->[-1];
  2031. } else {
  2032. $previous_ending_container = $previous;
  2033. }
  2034. my @gathered_index_entries;
  2035. #print STDERR "Gathering for item $item in previous $previous ($previous_ending_container)\n";
  2036. while ($previous_ending_container->{'contents'}->[-1]
  2037. and (($previous_ending_container->{'contents'}->[-1]->{'type'}
  2038. and $previous_ending_container->{'contents'}->[-1]->{'type'} eq 'index_entry_command')
  2039. or ($previous_ending_container->{'contents'}->[-1]->{'cmdname'}
  2040. and ($previous_ending_container->{'contents'}->[-1]->{'cmdname'} eq 'c'
  2041. or $previous_ending_container->{'contents'}->[-1]->{'cmdname'} eq 'comment')))) {
  2042. unshift @gathered_index_entries, pop @{$previous_ending_container->{'contents'}};
  2043. }
  2044. #print STDERR "Gathered: @gathered_index_entries\n";
  2045. if (scalar(@gathered_index_entries)) {
  2046. # put back leading comments
  2047. while ($gathered_index_entries[0]
  2048. and (!$gathered_index_entries[0]->{'type'}
  2049. or $gathered_index_entries[0]->{'type'} ne 'index_entry_command')) {
  2050. #print STDERR "Putting back $gathered_index_entries[0] $gathered_index_entries[0]->{'cmdname'}\n";
  2051. push @{$previous_ending_container->{'contents'}},
  2052. shift @gathered_index_entries;
  2053. }
  2054. # We have the index entries of the previous @item or before item.
  2055. # Now put them right after the current @item command.
  2056. if (scalar(@gathered_index_entries)) {
  2057. my $item_container;
  2058. if ($item->{'contents'} and $item->{'contents'}->[0]
  2059. and $item->{'contents'}->[0]->{'type'}
  2060. and $item->{'contents'}->[0]->{'type'} eq 'preformatted') {
  2061. $item_container = $item->{'contents'}->[0];
  2062. } else {
  2063. $item_container = $item;
  2064. }
  2065. foreach my $entry(@gathered_index_entries) {
  2066. $entry->{'parent'} = $item_container;
  2067. }
  2068. if ($item_container->{'contents'}
  2069. and $item_container->{'contents'}->[0]
  2070. and $item_container->{'contents'}->[0]->{'type'}) {
  2071. if ($item_container->{'contents'}->[0]->{'type'} eq 'empty_line_after_command') {
  2072. unshift @gathered_index_entries, shift @{$item_container->{'contents'}};
  2073. } elsif ($item_container->{'contents'}->[0]->{'type'} eq 'empty_spaces_after_command') {
  2074. unshift @gathered_index_entries, shift @{$item_container->{'contents'}};
  2075. $gathered_index_entries[0]->{'type'} = 'empty_line_after_command';
  2076. $gathered_index_entries[0]->{'text'} .= "\n";
  2077. }
  2078. }
  2079. unshift @{$item_container->{'contents'}}, @gathered_index_entries;
  2080. }
  2081. }
  2082. }
  2083. $previous = $item;
  2084. }
  2085. }
  2086. sub _move_index_entries_after_items($$$)
  2087. {
  2088. my $self = shift;
  2089. my $type = shift;
  2090. my $current = shift;
  2091. if ($current->{'cmdname'} and ($current->{'cmdname'} eq 'enumerate'
  2092. or $current->{'cmdname'} eq 'itemize')) {
  2093. move_index_entries_after_items($current);
  2094. }
  2095. return ($current);
  2096. }
  2097. sub move_index_entries_after_items_in_tree($)
  2098. {
  2099. my $tree = shift;
  2100. return modify_tree(undef, $tree, \&_move_index_entries_after_items);
  2101. }
  2102. 1;
  2103. __END__
  2104. =head1 NAME
  2105. Texinfo::Common - Classification of commands and miscellaneous methods
  2106. =head1 SYNOPSIS
  2107. use Texinfo::Common qw(expand_today expand_verbatiminclude);
  2108. if ($Texinfo::Common::accent_commands{$a_command}) {
  2109. print STDERR "$a_command is an accent command\n";
  2110. }
  2111. my $today_tree = expand_today($converter);
  2112. my $verbatiminclude_tree
  2113. = expand_verbatiminclude(undef, $verbatiminclude);
  2114. =head1 DESCRIPTION
  2115. Texinfo::Common holds interesting hashes classifying Texinfo @-commands,
  2116. as well as miscellaneous methods that may be useful for any backend
  2117. converting texinfo trees.
  2118. It also defines, as our variable a hash for default indices,
  2119. named C<%index_names>. The format of this hash is described in
  2120. L<Texinfo::Parser/indices_information>.
  2121. =head1 COMMAND CLASSES
  2122. Hashes are defined as C<our> variables, and are therefore available
  2123. outside of the module.
  2124. The key of the hashes are @-command names without the @. The
  2125. following hashes are available:
  2126. =over
  2127. =item %all_commands
  2128. All the @-commands.
  2129. =item %no_brace_commands
  2130. Commands without brace with a single character as name, like C<*>
  2131. or C<:>. The value is an ascii representation of the command. It
  2132. may be an empty string.
  2133. =item %misc_commands
  2134. Command that do not take braces and are not block commands either, like
  2135. C<@node>, C<@chapter>, C<@cindex>, C<@deffnx>, C<@end>, C<@footnotestyle>,
  2136. C<@set>, C<@settitle>, C<@indent>, C<@definfoenclose>, C<@comment> and many
  2137. others.
  2138. =item %default_index_commands
  2139. Index entry commands corresponding to default indices. For example
  2140. C<@cindex>.
  2141. =item %root_commands
  2142. Commands that are at the root of a Texinfo document, namely
  2143. C<@node> and sectioning commands, except heading commands.
  2144. =item %sectioning_commands
  2145. All the sectioning and heading commands.
  2146. =item %brace_commands
  2147. The commands that take braces. The associated value is the maximum
  2148. number of arguments.
  2149. =item %letter_no_arg_commands
  2150. @-commands with braces but no argument corresponding to letters,
  2151. like C<@AA{}> or C<@ss{}> or C<@o{}>.
  2152. =item %accent_commands
  2153. Accent @-commands taking an argument, like C<@'> or C<@ringaccent>
  2154. including C<@dotless> and C<@tieaccent>.
  2155. =item %style_commands
  2156. Commands that mark a fragment of texinfo, like C<@strong>,
  2157. C<@cite>, C<@code> or C<@asis>.
  2158. =item %code_style_commands
  2159. I<style_commands> that have their argument in code style, like
  2160. C<@code>.
  2161. =item %regular_font_style_commands
  2162. I<style_commands> that have their argument in regular font, like
  2163. C<@r> or C<@slanted>.
  2164. =item %context_brace_commands
  2165. @-commands with brace like C<@footnote>, C<@caption> and C<@math>
  2166. whose argument is outside of the main text flow in one way or another.
  2167. =item %ref_commands
  2168. Cross reference @-command referencing nodes, like C<@xref>.
  2169. =item %explained_commands
  2170. @-commands whose second argument explain first argument and further
  2171. @-command call without first argument, as C<@abbr> and C<@acronym>.
  2172. =item %block commands
  2173. Commands delimiting a block with a closing C<@end>. The value
  2174. is I<conditional> for C<@if> commands, I<def> for definition
  2175. commands like C<@deffn>, I<raw> for @-commands that have no expansion
  2176. of @-commands in their bodies and I<multitable> for C<@multitable>.
  2177. Otherwise it is set to the number of arguments separated by commas
  2178. that may appear on the @-command line. That means 0 in most cases,
  2179. 1 for C<@quotation> and 2 for C<@float>.
  2180. =item %raw_commands
  2181. @-commands that have no expansion of @-commands in their bodies,
  2182. as C<@macro>, C<@verbatim> or C<@ignore>.
  2183. =item %format_raw_commands
  2184. @-commands associated with raw output format, like C<@html>, or
  2185. C<@docbook>.
  2186. =item %texinfo_output_formats
  2187. Cannonical output formats that have associated conditionals. In
  2188. practice C<%format_raw_commands> plus C<info> and C<plaintext>.
  2189. =item %def_commands
  2190. =item %def_aliases
  2191. Definition commands. C<%def_aliases> associates an aliased command
  2192. to the original command, for example C<defun> is associated to C<deffn>.
  2193. =item %menu_commands
  2194. @-commands with menu entries.
  2195. =item %align_commands
  2196. @-commands related with alignement of text.
  2197. =item %region_commands
  2198. Block @-commands that enclose full text regions, like C<@titlepage>.
  2199. =item %preformatted_commands
  2200. =item %preformatted_code_commands
  2201. I<%preformatted_commands> is for commands whose content should not
  2202. be filled, like C<@example> or C<@display>. If the command is meant
  2203. for code, it is also in I<%preformatted_code_commands>, like C<@example>.
  2204. =item %item_container_commands
  2205. Commands holding C<@item> with C<@item> that contains blocks of text,
  2206. like C<@itemize>.
  2207. =item %item_line_commands
  2208. Commands with C<@item> that have their arguments on their lines, like
  2209. C<@ftable>.
  2210. =back
  2211. =head1 METHODS
  2212. No method is exported in the default case.
  2213. Most methods takes a I<$converter> as argument, sometime optionally,
  2214. to get some information and use methods for error reporting,
  2215. see L<Texinfo::Convert::Converter> and L<Texinfo::Report>.
  2216. =over
  2217. =item $tree = expand_today($converter)
  2218. Expand today's date, as a texinfo tree with translations.
  2219. =item $tree = expand_verbatiminclude($converter, $verbatiminclude)
  2220. The I<$converter> argument may be undef. I<$verbatiminclude> is a
  2221. C<@verbatiminclude> tree element. This function returns a
  2222. C<@verbatim> tree elements after finding the included file and
  2223. reading it. If I<$converter> is not defined, the document encoding
  2224. is not taken into account when reading the file.
  2225. =item $tree = definition_category($converter, $def_line)
  2226. The I<$converter> argument may be undef. I<$def_line> is a
  2227. C<def_line> texinfo tree container. This function
  2228. returns a texinfo tree corresponding to the category of the
  2229. I<$def_line> taking the class into account, if there is one.
  2230. If I<$converter> is not defined, the resulting string won't be
  2231. translated.
  2232. =item $result = is_content_empty($tree, $do_not_ignore_index_entries)
  2233. Return true if the C<$tree> has content that could be formatted.
  2234. C<$do_not_ignore_index_entries> is optional. If set, index entries
  2235. are considered to be formatted.
  2236. =item $result = numbered_heading ($converter, $heading_element, $heading_text, $do_number)
  2237. The I<$converter> argument may be undef. I<$heading_element> is
  2238. a heading command tree element. I<$heading_text> is the already
  2239. formatted heading text. if the I<$do_number> optional argument is
  2240. defined and false, no number is used and the text is returned as is.
  2241. This function returns the heading with a number and the appendix
  2242. part if needed. If I<$converter> is not defined, the resulting
  2243. string won't be translated.
  2244. =item ($caption, $prepended) = float_name_caption ($converter, $float)
  2245. I<$float> is a texinfo tree C<@float> element. This function
  2246. returns the caption that should be used for the float formatting
  2247. and the I<$prepended> texinfo tree combining the type and label
  2248. of the float.
  2249. =item $text = enumerate_item_representation($specification, $number)
  2250. This function returns the number or letter correponding to item
  2251. number I<$number> for an C<@enumerate> specification I<$specification>,
  2252. appearing on an C<@enumerate> line. For example
  2253. enumerate_item_representation('c', 3)
  2254. is C<e>.
  2255. =item trim_spaces_comment_from_content($contents)
  2256. Remove empty spaces after commands or braces at begin and
  2257. spaces and comments at end from a content array, modifying it.
  2258. =item $normalized_name = normalize_top_node_name ($node_string)
  2259. Normalize the node name string given in argument, by normalizing
  2260. Top node case.
  2261. =item protect_comma_in_tree($tree)
  2262. Protect comma characters, replacing C<,> with @comma{} in tree.
  2263. =item protect_colon_in_tree($tree)
  2264. =item protect_node_after_label_in_tree($tree)
  2265. Protect colon with C<protect_colon_in_tree> and characters that
  2266. are special in node names after a label in menu entries (tab
  2267. dot and comma) with C<protect_node_after_label_in_tree>.
  2268. The protection is achieved by putting protected characters
  2269. in C<@asis{}>.
  2270. =item $contents_result = protect_first_parenthesis ($contents)
  2271. Return a contents array reference with first parenthesis in the
  2272. contents array reference protected.
  2273. =item protect_hashchar_at_line_beginning($parser, $tree)
  2274. Protect hash character at beginning of line if the line is a cpp
  2275. line directive. The I<$parser> argument maybe undef, if it is
  2276. defined it is used for error reporting in case an hash character
  2277. could not be protected because it appeared in a raw environment.
  2278. =item move_index_entries_after_items_in_tree($tree)
  2279. In C<@enumerate> and C<@itemize> from the tree, move index entries
  2280. appearing just before C<@item> after the C<@item>. Comment lines
  2281. between index entries are moved too.
  2282. =item $command = find_parent_root_command($parser, $tree_element)
  2283. Find the parent root command of a tree element (sectioning command or node).
  2284. The C<$parser> argument is optional, it is used to continue
  2285. through C<@insertcopying> if in a C<@copying>.
  2286. =item valid_tree_transformation($name)
  2287. Return true if the I<$name> is a known tree transformation name
  2288. that may be passed with C<TREE_TRANSFORMATIONS> to modify a texinfo
  2289. tree.
  2290. =back
  2291. =head1 SEE ALSO
  2292. L<Texinfo::Parser>, L<Texinfo::Convert::Converter> and L<Texinfo::Report>.
  2293. =head1 AUTHOR
  2294. Patrice Dumas, E<lt>pertusus@free.frE<gt>
  2295. =head1 COPYRIGHT AND LICENSE
  2296. Copyright 2010, 2011, 2012 Free Software Foundation, Inc.
  2297. This library is free software; you can redistribute it and/or modify
  2298. it under the terms of the GNU General Public License as published by
  2299. the Free Software Foundation; either version 3 of the License,
  2300. or (at your option) any later version.
  2301. =cut