hxtool-conv.pl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/usr/bin/perl -w
  2. #
  3. # Script to convert .hx file STEXI/ETEXI blocks to SRST/ERST
  4. #
  5. # Copyright (C) 2020 Linaro
  6. #
  7. # This work is licensed under the terms of the GNU GPL, version 2 or
  8. # (at your option) any later version. See the COPYING file in the
  9. # top-level directory.
  10. # This script was only ever intended as a one-off conversion operation.
  11. # Please excuse the places where it is a bit hacky.
  12. # Some manual intervention after the conversion is expected, as are
  13. # some warnings from makeinfo.
  14. # Warning: this script is not idempotent: don't try to run it on
  15. # a .hx file that already has SRST/ERST sections.
  16. # Expected usage:
  17. # scripts/hxtool-conv.pl file.hx > file.hx.new
  18. use utf8;
  19. my $reading_texi = 0;
  20. my $texiblock = '';
  21. my @tables = ();
  22. sub update_tables($) {
  23. my ($texi) = @_;
  24. # Update our list of open table directives: every @table
  25. # line in the texi fragment is added to the list, and every
  26. # @end table line means we remove an entry from the list.
  27. # If this fragment had a completely self contained table with
  28. # both the @table and @end table lines, this will be a no-op.
  29. foreach (split(/\n/, $texi)) {
  30. push @tables, $_ if /^\@table/;
  31. pop @tables if /^\@end table/;
  32. }
  33. }
  34. sub only_table_directives($) {
  35. # Return true if every line in the fragment is a start or end table directive
  36. my ($texi) = @_;
  37. foreach (split(/\n/, $texi)) {
  38. return 0 unless /^\@table/ or /^\@end table/;
  39. }
  40. return 1;
  41. }
  42. sub output_rstblock($) {
  43. # Write the output to /tmp/frag.texi, wrapped in whatever current @table
  44. # lines we need.
  45. my ($texi) = @_;
  46. # As a special case, if this fragment is only table directives and
  47. # nothing else, update our set of open table directives but otherwise
  48. # ignore it. This avoids emitting an empty SRST/ERST block.
  49. if (only_table_directives($texi)) {
  50. update_tables($texi);
  51. return;
  52. }
  53. open(my $fragfh, '>', '/tmp/frag.texi');
  54. # First output the currently active set of open table directives
  55. print $fragfh join("\n", @tables);
  56. # Next, update our list of open table directives.
  57. # We need to do this before we emit the closing table directives
  58. # so that we emit the right number if this fragment had an
  59. # unbalanced set of directives.
  60. update_tables($texi);
  61. # Then emit the texi fragment itself.
  62. print $fragfh "\n$texi\n";
  63. # Finally, add the necessary closing table directives.
  64. print $fragfh "\@end table\n" x scalar @tables;
  65. close $fragfh;
  66. # Now invoke makeinfo/pandoc on it and slurp the results into a string
  67. open(my $fh, '-|', "makeinfo --force -o - --docbook "
  68. . "-D 'qemu_system_x86 QEMU_SYSTEM_X86_MACRO' "
  69. . "-D 'qemu_system QEMU_SYSTEM_MACRO' /tmp/frag.texi "
  70. . " | pandoc -f docbook -t rst")
  71. or die "can't start makeinfo/pandoc: $!";
  72. binmode $fh, ':encoding(utf8)';
  73. print "SRST\n";
  74. # Slurp the whole thing into a string so we can do multiline
  75. # string matches on it.
  76. my $rst = do {
  77. local $/ = undef;
  78. <$fh>;
  79. };
  80. $rst =~ s/^- − /- /gm;
  81. $rst =~ s/“/"/gm;
  82. $rst =~ s/”/"/gm;
  83. $rst =~ s/‘/'/gm;
  84. $rst =~ s/’/'/gm;
  85. $rst =~ s/QEMU_SYSTEM_MACRO/|qemu_system|/g;
  86. $rst =~ s/QEMU_SYSTEM_X86_MACRO/|qemu_system_x86|/g;
  87. $rst =~ s/(?=::\n\n +\|qemu)/.. parsed-literal/g;
  88. $rst =~ s/:\n\n::$/::/gm;
  89. # Fix up the invalid reference format makeinfo/pandoc emit:
  90. # `Some string here <#anchorname>`__
  91. # should be:
  92. # :ref:`anchorname`
  93. $rst =~ s/\`[^<`]+\<\#([^>]+)\>\`__/:ref:`$1`/gm;
  94. print $rst;
  95. close $fh or die "error on close: $!";
  96. print "ERST\n";
  97. }
  98. # Read the whole .hx input file.
  99. while (<>) {
  100. # Always print the current line
  101. print;
  102. if (/STEXI/) {
  103. $reading_texi = 1;
  104. $texiblock = '';
  105. next;
  106. }
  107. if (/ETEXI/) {
  108. $reading_texi = 0;
  109. # dump RST version of block
  110. output_rstblock($texiblock);
  111. next;
  112. }
  113. if ($reading_texi) {
  114. # Accumulate the texi into a string
  115. # but drop findex entries as they will confuse makeinfo
  116. next if /^\@findex/;
  117. $texiblock .= $_;
  118. }
  119. }
  120. die "Unexpectedly still in texi block at EOF" if $reading_texi;