-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrap ".rb
executable file
·58 lines (53 loc) · 1.16 KB
/
wrap ".rb
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
#! /usr/bin/env ruby
# encoding: UTF-8
# This wraps quotes (and others) around a selection
# In order:
# bare
# "bare"
# 'bare'
# "'bare'"
# '"bare"'
# [bare]
# {bare}
# {:bare}
# {"bare"}
# #{bare}
# "#{bare}"
# bare
original = ARGF.readlines.join
PRINTCHARS = /[[:print:]]+?/
DQ = /"/
SQ = /'/
B1 = /\{/
B2 = /\}/
HASH = /#/
SB1 = /\[/
SB2 = /\]/
print case original
# "#{bare}" -> bare # remove interpolation and quotes
when /^ #{DQ} #{HASH} #{B1} (#{PRINTCHARS}) #{B2} #{DQ} $/x
$1
# "bare" -> 'bare'
when /^ #{DQ} (?!')(#{PRINTCHARS})(?<!') #{DQ} $/x
%Q!'#{$1}'!
# "'bare'" -> '"bare"'
when /^ #{DQ} #{SQ} (#{PRINTCHARS}) #{SQ} #{DQ} $/x
%Q!'"#{$1}"'!
# '"bare"' -> [bare]
when /^ #{SQ} #{DQ} (#{PRINTCHARS}) #{DQ} #{SQ} $/x
%Q![#{$1}]!
# [bare] -> {bare}
when /^ #{SB1} ( #{PRINTCHARS} ) #{SB2} /x
%Q!{#{$1}}!
# {"bare"} -> #{bare}
when /^ #{B1} #{DQ} (#{PRINTCHARS}) #{DQ} #{B2} /x
%Q!\#{#{$1}}!
# {bare} -> {:bare}
when /^ #{B1} (?!\:)(#{PRINTCHARS}) #{B2} /x
%Q!{:#{$1}}!
# {:bare} -> {"bare"}
when /^ #{B1} \: (#{PRINTCHARS}) #{B2} /x
%Q!{"#{$1}"}!
else
%Q!"#{original}"!
end