orgcard2txt.pl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # orgcard2txt.pl - a script to generate orgcard.txt from orgcard.tex
  2. # Copyright (C) 2010, 2013 Osamu OKANO
  3. #
  4. # Version: 0.1
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. # Usage:
  20. # ======
  21. # perl orgcard2txt.pl orgcard.tex > orgcard.txt
  22. use strict;
  23. use warnings;
  24. sub rep_esc{
  25. my $s = shift @_;
  26. $s =~ s/\\kbd{([^}]+)}/$1/g;
  27. $s =~ s/\$\^([0-9])\$/[$1]/g;
  28. $s =~ s/\\rm //g;
  29. $s =~ s/\\\///g;
  30. $s =~ s/\\\^{}/^/g;
  31. $s =~ s/\\}/}/g;
  32. $s =~ s/\\{/{/g;
  33. $s =~ s/\\\#/#/g;
  34. $s =~ s/\\\^/^/g;
  35. $s =~ s/\\\%/%/g;
  36. $s =~ s/\\\_/_/g;
  37. $s =~ s/\\\&/&/g;
  38. $s =~ s/\\\$/\$/g;
  39. $s =~ s/\$\\leftrightarrow\$/<->/g;
  40. $s =~ s/\$\\pm 1\$/±1/g;
  41. $s =~ s/``{\\tt ([^}]+)}''/`$1'/g;
  42. return $s;
  43. }
  44. my $page=0;
  45. my $orgversionnumber;
  46. open(IN,$ARGV[0]);
  47. while(<IN>){
  48. last if(/\f/);
  49. $orgversionnumber = $1 if /\\def\\orgversionnumber{([^}]+)}/;
  50. }
  51. close(IN);
  52. print <<HEAD;
  53. ================================================================================
  54. Org Mode Reference Card (for version $orgversionnumber)
  55. ================================================================================
  56. HEAD
  57. my $key;
  58. my $value;
  59. format STDOUT =
  60. @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  61. $key,$value
  62. .
  63. open(IN,$ARGV[0]);
  64. while(<IN>){
  65. if(/\f/){
  66. $page = $page + 1;
  67. next;
  68. }
  69. next if($page != 1);
  70. next if(/^%/);
  71. next if /Org Mode Reference Card \([12]\/2\)/;
  72. next if /\\centerline{\(for version \\orgversionnumber\)}/;
  73. next if /\(for version \)/;
  74. next if /\\newcolumn/;
  75. next if /\\copyrightnotice/;
  76. next if /\\bye/;
  77. next if /\\title{([^}]+)}/;
  78. chomp;
  79. # print "b:$_\n";
  80. s/([^\\])\%.+$/$1/;
  81. # print "a:$_\n";
  82. if (/\\section{(.+)}/){
  83. my $sec = rep_esc($1);
  84. print "================================================================================\n";
  85. print "$sec\n";
  86. print "================================================================================\n";
  87. next;
  88. }
  89. if (/{\\bf (.+)}/){
  90. my $bf = rep_esc($1);
  91. print "--------------------------------------------------------------------------------\n";
  92. print "$bf\n";
  93. print "--------------------------------------------------------------------------------\n";
  94. next;
  95. }
  96. if (/^{\\it (.+)}/){
  97. my $it = rep_esc($1);
  98. print "--------------------------------------------------------------------------------\n";
  99. print "$it\n";
  100. print "--------------------------------------------------------------------------------\n";
  101. next;
  102. }
  103. if(/^\\key{(.+)}\s*$/||/^\\metax{(.+)}\s*$/){
  104. my ($k,$v) = split(/}{/,$1);
  105. my $k2 = &rep_esc($k);
  106. my $v2 = &rep_esc($v);
  107. # print "$k2\t$v2\n";
  108. ($key,$value)=($k2,$v2);
  109. write;
  110. next;
  111. }
  112. my $line = rep_esc($_);
  113. $line =~ s/{\\it ([^}]+)}/$1/g;
  114. $line =~ s/{\\tt ([^}]+)}/$1/g;
  115. print "$line\n";
  116. }
  117. close(IN);