summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/match_parens/match_parens
blob: 67f8ebe8f58c6b59df8900b3d048edbc47dc896d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env ruby
# encoding: utf-8

 MYNAME = File.basename($0)
Version = '1.41'

<<'DOC'
= match_parens - find mismatches of parentheses, braces, (angle) brackets, in texts

= Synopsis
match_parens [filename]	

= Description
Mismatches of parentheses, braces, (angle) brackets, especially in TeX
sources which may be rich in those, may be difficult to trace. This little
script helps you by writing your text to standard output, after adding a
left margin to your text, which will normally be almost empty, but will
clearly show any mismatches. (Just try me on myself to see that the
parenthesis starting this sentence will not appear to be matched at the end
of the file. If you look at me in the vim editor, then select this
paragraph and try the command |:!%|.

The exit value of the script is 0 when there are no mismatches, 1 otherwise.

Angle brackets are only looked for inside HTML text, where HTML is
supposed to start with |<html>| or |=begin rdoc| and to end with
|</html>| or |=end|.

= Examples
Suppose we have two files, |good| and |bad|, containing these texts:
	good:				bad:
	This is a (simple) test 	This is a (simple test
	without mismatches		containing mismatches

then here are some usage examples. First a simple test on these files:
	$ match_parens good
	      1 	This is a (simple) test
	      2 	without mismatches
	$ echo $?
	0
	$ match_parens bad
	      1 (	This is a (simple test
	      2 (	containing mismatches
	$ echo $?
	1

Just report if there are mismatches: 
	$ match_parens good >/dev/null && echo fine || echo problems
	fine
	$ match_parens bad >/dev/null && echo fine || echo problems
	problems

Report all tex files with mismatches in the current directory:
	$ for i in *.tex; do match_parens $i >/dev/null || echo $i; done

Matches must be in correct order:
	$ echo -e "This is a ([simple)] test\n" | match_parens
	      1 ([)]	This is a ([simple)] test
	      2 ([)]	
= Bugs
Conversion of ``...'' to “...” should only be done in latex files

= See also
matchparens: the Bash version, which is 10+ times slower

= Changes
Changes with respect to version 1.4:
- clearer output
- added --help and --version options
- documentation adapted to gendoc

= Author and copyright
Author	Wybo Dekker
Email	U{Wybo@dekkerdocumenten.nl}{wybo@dekkerdocumenten.nl}
License	Released under the U{www.gnu.org/copyleft/gpl.html}{GNU General Public License}
DOC

require 'optparse'

ARGV.options do |opt|
  opt.banner = "#{MYNAME} - find mismatches of parentheses, braces, (angle) brackets, in texts\n"
  opt.on('-V','--version',
    'print version and exit') do
      puts Version
      exit
    end
  opt.on('-h','--help',
    'print this help and exit') do
      puts opt.to_s.sub(/^ *-I\n/,'')
      exit
    end
  opt.on('-I') do system("instscript -zp #{MYNAME}"); exit; end
  opt.parse!
end
      s=''
      n=0
   html=false
 parens='{}[]()“”'

arg = ARGV[0] || ''
unless arg.empty?
  test(?e,arg) or raise("file #{arg} does not exist")
  test(?r,arg) or raise("file #{arg} is not readable")
end
while x = gets()
  x = x.gsub(/``/,'“').gsub(/''/,'”')

  if html && (x=~/^([# ]*=end)/ || x=~/<\/html>/i)
         html = false
         parens = '{}[]()'
  elsif !html && (x=~/^([# ]*=begin rdoc)/ || x=~/<html>/i)
         html = true
         parens = '{}[]()“”<>'
  end   

  # remove {x} where x in []{}()<>, copy all parens to s
  s << x.gsub(/“\{[\[\]{}()<>]\}”/,'').tr('^'+parens,'')
  # remove matches from inside
  while s.gsub!(/\{\}|\(\)|\[\]|<>|“”/,'') do end
  puts "%4d | %-10s | %s" % [n+=1,s.slice(0..9),x]
end
exit s.empty? ? 0 : 1