-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathparagraphs.sed
More file actions
81 lines (71 loc) · 1.73 KB
/
paragraphs.sed
File metadata and controls
81 lines (71 loc) · 1.73 KB
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
# This uses a neat trick from the Sed & Awk Book from O'Reilly:
# Ensure that the last line looks like the end of a paragraph; if it isn't
# blank, hold it and blank it out.
${
/^$/!{
H
s/.*//
}
}
# Non-blank lines get held in the hold buffer
/^$/!{
H
d
}
# Blank lines signify the end of a paragraph.
# Swap blank into hold buffer (bringing paragraph into pattern buffer).
# Move newline prefix to end of paragraph.
/^$/{
x
s/^\(\n\)\(.*\)/\2\1/
}
# Each P inside of :: represents a paragraph that we need to acquire. When we
# see two or more P, we reduce the number by one, append the recently acquired
# paragraph to the pattern space, then hold the whole thing and delete the
# pattern space. This puts the most recent paragraph into the hold buffer and
# starts the process over with paragraph accumulation.
# When discovering that we need, e.g., three more paragraphs, we can do the
# following:
#
# s/^/::PPP::/
# G
# h
# d
#
# This prefixes the current paragraph with the number of paragraphs needed,
# appends the contents of the hold space (a single newline after paragraph
# processing), puts everything back into the hold space, and deletes the
# pattern space (looping back to the beginning of the script, where paragraph
# processing begins). See "needonemore" for a convenience label that you can
# branch to for adding one paragraph.
/^::P\(P\{1,\}\)::/{
s//::\1::/
G
h
d
}
# If we have exactly one P, then we swap it out for a zero and let processing
# continue (after :start).
/^::P::/{
s//::0::/
G
}
b start
# Convenience subroutines
:needonemore
s/^/::P::/
G
h
d
:needtwomore
s/^/::PP::/
G
h
d
:needthreemore
s/^/::PPP::/
G
h
d
# Start regular processing (this file is intended to be a preamble)
:start