-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathsimple_quoter
executable file
·60 lines (48 loc) · 1.2 KB
/
simple_quoter
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
#!/usr/bin/env perl
# This is a short script that tries to automatically
# insert curling quotes.
# By David A. Wheeler
# Copyright the Linux Foundation
# SPDX-License-Identifier: MIT
use utf8;
use open qw( :std :encoding(UTF-8) );
use feature 'unicode_strings';
$in_pre = 0;
while (<>) {
# Handle markdown ~~~~
if (/^~~~~/) {
$in_pre = !$in_pre;
print;
next;
}
if ($in_pre) {
print;
next;
}
# Fix curling quotes like “a” and ‘a’
# Single quotes begin/end a word
s/^'/‘/;
s/(<br>|[ \(\[\{])(\**)'/\1\2‘/g;
s/'$/’/;
s/'(\**)([ ,.\)\]\}\:])/’\1\2/g;
# Contractions
s/n't/n’t/;
s/([A-Za-z])'s/\1’s/;
# Double quotes
# space * "
s/^(\**)"/\1“/;
s/(<br>|[ \(\[\{])(\**)"/\1\2“/g;
s/"(\**)$/”\1/;
s/"(\**)([ ,.\)\]\}])/”\1\2/g;
# If they're lonely, convert them back
s/ [“”] / " /g;
s/ [‘’] / ' /g;
# Fix URLs / HTML
s/["“”](https?:\/\/[^ "'“”‘’]*)["“”]/"\1"/g;
s/['‘’](https?:\/\/[^ "'“”‘’]*)['‘’]/'\1'/g;
s/ ([a-z]+)=["“”]([^ "“”]*)["“”]/ \1="\2"/g;
s/ ([a-z]+)=['‘’]([^ '‘’]*)['‘’]/ \1='\2'/g;
s/["“”]>/">/g;
s/['‘’]>/'>/g;
print;
}