tagged release 0.6.4
[parrot.git] / apps / p3 / cgi-pir / slides.pir
blob50fed7d53005215992ba7ad384746b9fd813dbfe
1 .sub cgi_main
2     .param pmc    conn
3     .param string query
4     .param pmc    query_hash
5     ##trace 1
7     .local string file, res, slides
8     file = query_hash['f']
9     res = 'file nof found'
10     res .= file
11     slides = _read_file(conn, file)
12     unless slides goto err
13     .local int p
14     p = query_hash['p']
15     res = _find_slides(conn, slides, p)
16 err:
17     trace 0
18     .return (res)
19 .end
21 .sub _read_file
22     .param pmc conn
23     .param string file
24     $P0 = getclass 'ParrotIO'
25     $S0 = $P0.'slurp'(file)
26     .return ($S0)
27 .end
29 .sub _find_slides
30     .param pmc conn
31     .param string slides
32     .param int part
34     .local int n, p, pos1, pos2
35     .local string tag
36     n = 0
37     pos1 = index slides, '<body', 0
38     if pos1 == -1 goto nobody
39     pos1 += 6
40     goto loop
41 nobody:
42     pos1 = 0
43 loop:
44     (pos1, p, $S0) = _next_tag(slides, pos1)
45     if p == -1 goto serve_eof   # at eof
46 loop2:
47     (pos2, p, tag) = _next_tag(slides, p)
48     if p == -1 goto serve_eof   # at eof
49     if part == n goto serve_it
50     if tag == 'ul' goto loop2
51     if tag == 'ol' goto loop2
52     if tag == 'dl' goto loop2
53     if tag == 'blockquote' goto loop2
54     inc n
55     if tag == 'p' goto loop2
56     if tag == 'pre' goto loop2
57     if tag == 'h2' goto loop2
58     if tag == 'h3' goto loop2
59     if tag == 'li' goto loop2
60     if tag == 'dt' goto loop2
61     if tag == 'dd' goto loop2
62     pos1 = pos2
63     goto loop
65 serve_eof:
66     $S0 = '<h1>ENOMORESLIDES - <sub>'
67     $S1 = part
68     $S0 .= $S1
69     $S0 .= '</sub></h1>'
70     goto ret_it
71 serve_it:
72     $I0 = pos2 - pos1
73     $S0 = substr slides, pos1, $I0
74 ret_it:
75     .local pmc srv
76     if null conn goto no_conn
77     srv = conn.'server'()
78     srv.'debug'("CGI: pos1 ", pos1, " pos2 ", p, " -: ", $S0)
79 no_conn:
80     .return ($S0)
81 .end
83 .sub _next_tag
84     .param string slides
85     .param int pos1
87     .local int p, p1
88 loop:
89     p = index slides, '<', pos1
90     if p == -1 goto eof
91     p1 = p + 1
92     $S0 = slides[p1]
93     if $S0 == '/' goto ignore
94     # got open tag - ignore <b>, <i>, <tt>, <sup>, <sub> ...
95     if $S0 == 'b' goto ignore
96     if $S0 == 'i' goto ignore
97     if $S0 == 's' goto ignore
98     if $S0 == 't' goto ignore
99     pos1 = p
100     p = index slides, '>', p1
101     .local string tag
102     $I0 = p - p1
103     tag = substr slides, p1, $I0
104     inc p
105     .return (pos1, p, tag)
107 ignore:
108     pos1 = p1
109     goto loop
110 eof:
111     p = index slides, '</body>', pos1
112     .return (pos1, p, '')
113 .end
115 # DEBUG HELPER
116 .sub test :main
117     .param pmc argv
118     null $P0
119     $P1 = new 'Hash'
120     $P1['f'] = '1.html'
121     $S0 = argv[1]
122     $I0 = $S0
123     $P1['p'] = $I0
124     $S0 = cgi_main($P0, '', $P1)
125     print $S0
126 .end
128 # Local Variables:
129 #   mode: pir
130 #   fill-column: 100
131 # End:
132 # vim: expandtab shiftwidth=4 ft=pir: