PageRenderTime 8ms CodeModel.GetById 2ms app.highlight 2ms RepoModel.GetById 2ms app.codeStats 0ms

/paragraphs.sed

https://code.google.com/p/latex-makefile/
sed | 81 lines | 41 code | 10 blank | 30 comment | 0 complexity | 9b72386b72fddc1d85bbd4f5c0461c95 MD5 | raw file
 1# This uses a neat trick from the Sed & Awk Book from O'Reilly:
 2# Ensure that the last line looks like the end of a paragraph; if it isn't
 3# blank, hold it and blank it out.
 4${
 5  /^$/!{
 6    H
 7    s/.*//
 8  }
 9}
10# Non-blank lines get held in the hold buffer
11/^$/!{
12  H
13  d
14}
15# Blank lines signify the end of a paragraph.
16# Swap blank into hold buffer (bringing paragraph into pattern buffer).
17# Move newline prefix to end of paragraph.
18/^$/{
19  x
20  s/^\(\n\)\(.*\)/\2\1/
21}
22
23# Each P inside of :: represents a paragraph that we need to acquire.  When we
24# see two or more P, we reduce the number by one, append the recently acquired
25# paragraph to the pattern space, then hold the whole thing and delete the
26# pattern space.  This puts the most recent paragraph into the hold buffer and
27# starts the process over with paragraph accumulation.
28# When discovering that we need, e.g., three more paragraphs, we can do the
29# following:
30#
31# s/^/::PPP::/
32# G
33# h
34# d
35#
36# This prefixes the current paragraph with the number of paragraphs needed,
37# appends the contents of the hold space (a single newline after paragraph
38# processing), puts everything back into the hold space, and deletes the
39# pattern space (looping back to the beginning of the script, where paragraph
40# processing begins).  See "needonemore" for a convenience label that you can
41# branch to for adding one paragraph.
42
43/^::P\(P\{1,\}\)::/{
44  s//::\1::/
45  G
46  h
47  d
48}
49
50# If we have exactly one P, then we swap it out for a zero and let processing
51# continue (after :start).
52
53/^::P::/{
54  s//::0::/
55  G
56}
57
58b start
59
60# Convenience subroutines
61
62:needonemore
63s/^/::P::/
64G
65h
66d
67
68:needtwomore
69s/^/::PP::/
70G
71h
72d
73
74:needthreemore
75s/^/::PPP::/
76G
77h
78d
79
80# Start regular processing (this file is intended to be a preamble)
81:start