~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to MoinMoin/macro/Include.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mfrom: (0.9.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080622211713-fpo2zrq3s5dfecxg
Tags: 1.7.0-3
Simplify /etc/moin/wikilist format: "USER URL" (drop unneeded middle
CONFIG_DIR that was wrongly advertised as DATA_DIR).  Make
moin-mass-migrate handle both formats and warn about deprecation of
the old one.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
        http://purl.net/wiki/moinmaster/HelpOnMacros/Include
8
8
 
9
9
    for detailed docs.
10
 
    
11
 
    @copyright: 2000-2004 by J�rgen Hermann <jh@web.de>
12
 
    @copyright: 2000-2001 by Richard Jones <richard@bizarsoftware.com.au>
 
10
 
 
11
    @copyright: 2000-2004 Juergen Hermann <jh@web.de>,
 
12
                2000-2001 Richard Jones <richard@bizarsoftware.com.au>
13
13
    @license: GNU GPL, see COPYING for details.
14
14
"""
15
15
 
16
16
#Dependencies = ["pages"] # included page
17
17
Dependencies = ["time"] # works around MoinMoinBugs/TableOfContentsLacksLinks
18
18
 
 
19
generates_headings = True
 
20
 
19
21
import re, StringIO
20
22
from MoinMoin import wikiutil
21
23
from MoinMoin.Page import Page
22
 
from MoinMoin.util import web
 
24
 
23
25
 
24
26
_sysmsg = '<p><strong class="%s">%s</strong></p>'
25
27
 
44
46
    for title, _ in title_re.findall(body):
45
47
        h = title.strip()
46
48
        level = 1
47
 
        while h[level:level+1] == '=': level = level+1
48
 
        depth = min(5,level)
 
49
        while h[level:level+1] == '=':
 
50
            level += 1
49
51
        title_text = h[level:-level].strip()
50
52
        titles.append((title_text, level))
51
53
    return titles
52
54
 
53
 
def execute(macro, text, args_re=re.compile(_args_re_pattern), title_re=re.compile(_title_re, re.M), called_by_toc=0):
 
55
def execute(macro, text, args_re=re.compile(_args_re_pattern), title_re=re.compile(_title_re, re.M)):
54
56
    request = macro.request
55
57
    _ = request.getText
56
58
 
61
63
    # parse and check arguments
62
64
    args = text and args_re.match(text)
63
65
    if not args:
64
 
        return (_sysmsg % ('error', _('Invalid include arguments "%s"!')) % (text,))
 
66
        return (_sysmsg % ('error', _('Invalid include arguments "%s"!')) % (text, ))
65
67
 
66
68
    # prepare including page
67
69
    result = []
68
 
    print_mode = macro.form.has_key('action') and macro.form['action'][0] in ("print", "format")
 
70
    print_mode = request.action in ("print", "format")
69
71
    this_page = macro.formatter.page
70
72
    if not hasattr(this_page, '_macroInclude_pagelist'):
71
73
        this_page._macroInclude_pagelist = {}
72
74
 
73
75
    # get list of pages to include
74
 
    inc_name = wikiutil.AbsPageName(request, this_page.page_name, args.group('name'))
 
76
    inc_name = wikiutil.AbsPageName(this_page.page_name, args.group('name'))
75
77
    pagelist = [inc_name]
76
78
    if inc_name.startswith("^"):
77
79
        try:
101
103
    for inc_name in pagelist:
102
104
        if not request.user.may.read(inc_name):
103
105
            continue
104
 
        if this_page._macroInclude_pagelist.has_key(inc_name):
105
 
            result.append(u'<p><strong class="error">Recursive include of "%s" forbidden</strong></p>' % (inc_name,))
 
106
        if inc_name in this_page._macroInclude_pagelist:
 
107
            result.append(u'<p><strong class="error">Recursive include of "%s" forbidden</strong></p>' % (inc_name, ))
106
108
            continue
107
109
        if skipitems:
108
110
            skipitems -= 1
122
124
        if from_re:
123
125
            try:
124
126
                from_match = re.compile(from_re, re.M).search(body)
125
 
            except re.error, e:
 
127
            except re.error:
126
128
                ##result.append("*** fe=%s ***" % e)
127
129
                from_match = re.compile(re.escape(from_re), re.M).search(body)
128
130
            if from_match:
141
143
                result.append(_sysmsg % ('warning', 'Include: ' + _('Nothing found for "%s"!')) % to_re)
142
144
 
143
145
        if titlesonly:
144
 
            newbody = []
145
146
            levelstack = []
146
147
            for title, level in extract_titles(body[from_pos:to_pos], title_re):
147
148
                if levelstack:
171
172
        ##result.append("*** f=%s t=%s ***" % (from_re, to_re))
172
173
        ##result.append("*** f=%d t=%d ***" % (from_pos, to_pos))
173
174
 
174
 
        if called_by_toc:
175
 
            result.append(inc_page.get_raw_body())
176
 
            continue
177
 
 
178
175
        if not hasattr(request, "_Include_backto"):
179
176
            request._Include_backto = this_page.page_name
180
 
        
 
177
 
181
178
        # do headings
182
179
        level = None
183
180
        if args.group('heading') and args.group('hquote'):
184
 
            heading = args.group('htext') or inc_page.split_title(request)
 
181
            heading = args.group('htext') or inc_page.split_title()
185
182
            level = 1
186
183
            if args.group('level'):
187
184
                level = int(args.group('level'))
190
187
                              macro.formatter.text(heading) +
191
188
                              macro.formatter.heading(0, level))
192
189
            else:
193
 
                import sha
194
 
                from MoinMoin import config
195
 
                # this heading id might produce duplicate ids,
196
 
                # if the same page is included multiple times
197
 
                # Encode stuf we feed into sha module.
198
 
                pntt = (inc_name + heading).encode(config.charset)
199
 
                hid = "head-" + sha.new(pntt).hexdigest()
200
 
                request._page_headings.setdefault(pntt, 0)
201
 
                request._page_headings[pntt] += 1
202
 
                if request._page_headings[pntt] > 1:
203
 
                    hid += '-%d'%(request._page_headings[pntt],)
204
 
                result.append(
205
 
                    #macro.formatter.heading(1, level, hid,
206
 
                    #    icons=edit_icon.replace('<img ', '<img align="right" ')) +
207
 
                    macro.formatter.heading(1, level, id=hid) +
208
 
                    inc_page.link_to(request, heading, css_class="include-heading-link") +
209
 
                    macro.formatter.heading(0, level)
210
 
                )
 
190
                url = inc_page.url(request)
 
191
                result.extend([
 
192
                    macro.formatter.heading(1, level, id=heading),
 
193
                    macro.formatter.url(1, url, css="include-heading-link"),
 
194
                    macro.formatter.text(heading),
 
195
                    macro.formatter.url(0),
 
196
                    macro.formatter.heading(0, level),
 
197
                ])
211
198
 
212
199
        # set or increment include marker
213
200
        this_page._macroInclude_pagelist[inc_name] = \
217
204
        strfile = StringIO.StringIO()
218
205
        request.redirect(strfile)
219
206
        try:
220
 
            cid = request.makeUniqueID("Include_%s" % wikiutil.quoteWikinameURL(inc_page.page_name))
221
 
            inc_page.send_page(request, content_only=1, content_id=cid,
222
 
                               omit_footnotes=True)
 
207
            inc_page.send_page(content_only=True,
 
208
                               omit_footnotes=True,
 
209
                               count_hit=False)
223
210
            result.append(strfile.getvalue())
224
211
        finally:
225
212
            request.redirect()
235
222
        if editlink and not (level or print_mode):
236
223
            result.extend([
237
224
                macro.formatter.div(1, css_class="include-link"),
238
 
                inc_page.link_to(request, '[%s]' % (inc_name,), css_class="include-page-link"),
239
 
                inc_page.link_to(request, '[%s]' % (_('edit'),), css_class="include-edit-link", querystr={'action': 'edit', 'backto': request._Include_backto}),
 
225
                inc_page.link_to(request, '[%s]' % (inc_name, ), css_class="include-page-link"),
 
226
                inc_page.link_to(request, '[%s]' % (_('edit'), ), css_class="include-edit-link", querystr={'action': 'edit', 'backto': request._Include_backto}),
240
227
                macro.formatter.div(0),
241
228
            ])
242
229
        # XXX page.link_to is wrong now, it escapes the edit_icon html as it escapes normal text