Back to index

moin  1.9.0~rc2
Public Member Functions | Public Attributes
MoinMoin.parser.text_rst.MoinTranslator Class Reference

List of all members.

Public Member Functions

def __init__
def depart_docinfo
def capture_wiki_formatting
def process_wiki_text
def add_wiki_markup
def astext
def fixup_wiki_formatting
def visit_reference
def visit_image
def create_wiki_functor
def setup_wiki_handlers
def visit_enumerated_list
def depart_enumerated_list
def create_admonition_functor
def setup_admonitions_handlers

Public Attributes

 formatter
 request
 wikiparser
 initial_header_level
 wiki_text
 body

Detailed Description

Definition at line 252 of file text_rst.py.


Constructor & Destructor Documentation

def MoinMoin.parser.text_rst.MoinTranslator.__init__ (   self,
  document,
  formatter,
  request,
  parser,
  writer 
)

Definition at line 254 of file text_rst.py.

00254 
00255     def __init__(self, document, formatter, request, parser, writer):
00256         html4css1.HTMLTranslator.__init__(self, document)
00257         self.formatter = formatter
00258         self.request = request
00259         # Using our own writer when needed. Save the old one to restore
00260         # after the page has been processed by the html4css1 parser.
00261         self.original_write, self.request.write = self.request.write, self.capture_wiki_formatting
00262         self.wikiparser = parser
00263         self.wikiparser.request = request
00264         # MoinMoin likes to start the initial headers at level 3 and the title
00265         # gets level 2, so to comply with their styles, we do here also.
00266         # TODO: Could this be fixed by passing this value in settings_overrides?
00267         self.initial_header_level = 3
00268         # Temporary place for wiki returned markup. This will be filled when
00269         # replacing the default writer with the capture_wiki_formatting
00270         # function (see visit_image for an example).
00271         self.wiki_text = ''
00272         self.setup_wiki_handlers()
00273         self.setup_admonitions_handlers()
00274 
00275         # Make all internal lists RawHTMLLists, see RawHTMLList class
00276         # comment for more information.
00277         for i in self.__dict__:
00278             if isinstance(getattr(self, i), list):
00279                 setattr(self, i, RawHTMLList(formatter))


Member Function Documentation

    Place holder in case this becomes more elaborate someday. For now it
    only appends the MoinMoin generated markup to the html body and
    raises SkipNode.

Definition at line 314 of file text_rst.py.

00314 
00315     def add_wiki_markup(self):
00316         """
00317             Place holder in case this becomes more elaborate someday. For now it
00318             only appends the MoinMoin generated markup to the html body and
00319             raises SkipNode.
00320         """
00321         self.body.append(self.wiki_text)
00322         self.wiki_text = ''
00323         raise docutils.nodes.SkipNode

Here is the caller graph for this function:

Definition at line 324 of file text_rst.py.

00324 
00325     def astext(self):
00326         self.request.write = self.original_write
00327         return html4css1.HTMLTranslator.astext(self)

    Captures MoinMoin generated markup to the instance variable
    wiki_text.

Definition at line 288 of file text_rst.py.

00288 
00289     def capture_wiki_formatting(self, text):
00290         """
00291             Captures MoinMoin generated markup to the instance variable
00292             wiki_text.
00293         """
00294         # For some reason getting empty strings here which of course overwrites
00295         # what we really want (this is called multiple times per MoinMoin
00296         # format call, which I don't understand).
00297         self.wiki_text += text

Definition at line 498 of file text_rst.py.

00498 
00499     def create_admonition_functor(self, admotion_class):
00500         def visit_func(self, node):
00501             self.wiki_text = ''
00502             self.request.write(self.formatter.div(1,
00503                                                   attr={'class': admotion_class},
00504                                                   allowed_attrs=[]))
00505             self.body.append(self.wiki_text)
00506         def depart_func(self, node):
00507             self.wiki_text = ''
00508             self.request.write(self.formatter.div(0))
00509             self.body.append(self.wiki_text)
00510 
00511         return visit_func, depart_func

Here is the caller graph for this function:

Definition at line 441 of file text_rst.py.

00441 
00442     def create_wiki_functor(self, moin_func):
00443         moin_callable = getattr(self.formatter, moin_func)
00444         def visit_func(self, node):
00445             self.wiki_text = ''
00446             self.request.write(moin_callable(1))
00447             self.body.append(self.wiki_text)
00448         def depart_func(self, node):
00449             self.wiki_text = ''
00450             self.request.write(moin_callable(0))
00451             self.body.append(self.wiki_text)
00452         return visit_func, depart_func

Here is the caller graph for this function:

    depart_docinfo assigns a new list to self.body, we need to re-make that
    into a RawHTMLList.

Definition at line 280 of file text_rst.py.

00280 
00281     def depart_docinfo(self, node):
00282         """
00283             depart_docinfo assigns a new list to self.body, we need to re-make that
00284             into a RawHTMLList.
00285         """
00286         html4css1.HTMLTranslator.depart_docinfo(self, node)
00287         self.body = RawHTMLList(self.formatter)

Definition at line 492 of file text_rst.py.

00492 
00493     def depart_enumerated_list(self, node):
00494         self.wiki_text = ''
00495         self.request.write(self.formatter.number_list(0))
00496         self.body.append(self.wiki_text)

Definition at line 328 of file text_rst.py.

00328 
00329     def fixup_wiki_formatting(self, text):
00330         replacement = {'\n': '', '> ': '>'}
00331         for src, dst in replacement.items():
00332             text = text.replace(src, dst)
00333         # Fixup extraneous markup
00334         # Removes any empty span tags
00335         text = re.sub(r'\s*<\s*span.*?>\s*<\s*/\s*span\s*>', '', text)
00336         # Removes the first paragraph tag
00337         text = re.sub(r'^\s*<\s*p[^>]*?>', '', text)
00338         # Removes the ending paragraph close tag and any remaining whitespace
00339         text = re.sub(r'<\s*/\s*p\s*>\s*$', '', text)
00340         return text

Here is the caller graph for this function:

    This sequence is repeated numerous times, so its captured as a
    single call here. Its important that wiki_text is blanked before we
    make the format call. format will call request.write which we've
    hooked to capture_wiki_formatting. If wiki_text is not blanked
    before a call to request.write we will get the old markup as well as
    the newly generated markup.

    TODO: Could implement this as a list so that it acts as a stack. I
    don't like having to remember to blank wiki_text.

Definition at line 298 of file text_rst.py.

00298 
00299     def process_wiki_text(self, text):
00300         """
00301             This sequence is repeated numerous times, so its captured as a
00302             single call here. Its important that wiki_text is blanked before we
00303             make the format call. format will call request.write which we've
00304             hooked to capture_wiki_formatting. If wiki_text is not blanked
00305             before a call to request.write we will get the old markup as well as
00306             the newly generated markup.
00307 
00308             TODO: Could implement this as a list so that it acts as a stack. I
00309             don't like having to remember to blank wiki_text.
00310         """
00311         self.wiki_text = ''
00312         self.wikiparser.raw = text
00313         self.wikiparser.format(self.formatter)

Here is the caller graph for this function:

    Admonitions are handled here... We basically surround admonitions
    in a div with class admonition_{name of the admonition}.

Definition at line 512 of file text_rst.py.

00512 
00513     def setup_admonitions_handlers(self):
00514         """
00515             Admonitions are handled here... We basically surround admonitions
00516             in a div with class admonition_{name of the admonition}.
00517         """
00518         handled_admonitions = [
00519             'attention',
00520             'caution',
00521             'danger',
00522             'error',
00523             'hint',
00524             'important',
00525             'note',
00526             'tip',
00527             'warning',
00528         ]
00529         for adm in handled_admonitions:
00530             visit_func, depart_func = self.create_admonition_functor(adm)
00531             visit_func = new.instancemethod(visit_func, self, MoinTranslator)
00532             depart_func = new.instancemethod(depart_func, self, MoinTranslator)
00533             setattr(self, 'visit_%s' % (adm), visit_func)
00534             setattr(self, 'depart_%s' % (adm), depart_func)
00535 

Here is the call graph for this function:

    Have the MoinMoin formatter handle markup when it makes sense. These
    are portions of the document that do not contain reST specific
    markup. This allows these portions of the document to look
    consistent with other wiki pages.

    Setup dispatch routines to handle basic document markup. The
    hanlders dict is the html4css1 handler name followed by the wiki
    handler name.

Definition at line 453 of file text_rst.py.

00453 
00454     def setup_wiki_handlers(self):
00455         """
00456             Have the MoinMoin formatter handle markup when it makes sense. These
00457             are portions of the document that do not contain reST specific
00458             markup. This allows these portions of the document to look
00459             consistent with other wiki pages.
00460 
00461             Setup dispatch routines to handle basic document markup. The
00462             hanlders dict is the html4css1 handler name followed by the wiki
00463             handler name.
00464         """
00465         handlers = {
00466             # Text Markup
00467             'emphasis': 'emphasis',
00468             'strong': 'strong',
00469             'literal': 'code',
00470             # Blocks
00471             'literal_block': 'preformatted',
00472             # Simple Lists
00473             # bullet-lists are handled completely by docutils because it uses
00474             # the node context to decide when to make a compact list
00475             # (no <p> tags).
00476             'list_item': 'listitem',
00477             # Definition List
00478             'definition_list': 'definition_list',
00479         }
00480         for rest_func, moin_func in handlers.items():
00481             visit_func, depart_func = self.create_wiki_functor(moin_func)
00482             visit_func = new.instancemethod(visit_func, self, MoinTranslator)
00483             depart_func = new.instancemethod(depart_func, self, MoinTranslator)
00484             setattr(self, 'visit_%s' % (rest_func), visit_func)
00485             setattr(self, 'depart_%s' % (rest_func), depart_func)

Here is the call graph for this function:

Definition at line 487 of file text_rst.py.

00487 
00488     def visit_enumerated_list(self, node):
00489         self.wiki_text = ''
00490         self.request.write(self.formatter.number_list(1, start=node.get('start', None)))
00491         self.body.append(self.wiki_text)

    Need to intervene in the case of inline images. We need MoinMoin to
    give us the actual src line to the image and then we can feed this
    to the default html4css1 writer. NOTE: Since the writer can't "open"
    this image the scale attribute doesn't work without directly
    specifying the height or width (or both).

    TODO: Need to handle figures similarly.

Definition at line 409 of file text_rst.py.

00409 
00410     def visit_image(self, node):
00411         """
00412             Need to intervene in the case of inline images. We need MoinMoin to
00413             give us the actual src line to the image and then we can feed this
00414             to the default html4css1 writer. NOTE: Since the writer can't "open"
00415             this image the scale attribute doesn't work without directly
00416             specifying the height or width (or both).
00417 
00418             TODO: Need to handle figures similarly.
00419         """
00420         uri = node['uri'].lstrip()
00421         prefix = ''          # assume no prefix
00422         attach_name = uri
00423         if ':' in uri:
00424             prefix = uri.split(':', 1)[0]
00425             attach_name = uri.split(':', 1)[1]
00426         # if prefix isn't URL, try to display in page
00427         if not prefix.lower() in ('file', 'http', 'https', 'ftp'):
00428             if not AttachFile.exists(self.request, self.request.page.page_name, attach_name):
00429                 # Attachment doesn't exist, MoinMoin should process it
00430                 if prefix == '':
00431                     prefix = 'attachment:'
00432                 self.process_wiki_text("{{%s%s}}" % (prefix, attach_name))
00433                 self.wiki_text = self.fixup_wiki_formatting(self.wiki_text)
00434                 self.add_wiki_markup()
00435             # Attachment exists, get a link to it.
00436             # create the url
00437             node['uri'] = AttachFile.getAttachUrl(self.request.page.page_name, attach_name, self.request, addts=1)
00438             if not node.hasattr('alt'):
00439                 node['alt'] = node.get('name', uri)
00440         html4css1.HTMLTranslator.visit_image(self, node)

Here is the call graph for this function:

    Pass links to MoinMoin to get the correct wiki space url. Extract
    the url and pass it on to the html4css1 writer to handle. Inline
    images are also handled by visit_image. Not sure what the "drawing:"
    link scheme is used for, so for now it is handled here.

    Also included here is a hack to allow MoinMoin macros. This routine
    checks for a link which starts with "<<". This link is passed to the
    MoinMoin formatter and the resulting markup is inserted into the
    document in the place of the original link reference.

Definition at line 341 of file text_rst.py.

00341 
00342     def visit_reference(self, node):
00343         """
00344             Pass links to MoinMoin to get the correct wiki space url. Extract
00345             the url and pass it on to the html4css1 writer to handle. Inline
00346             images are also handled by visit_image. Not sure what the "drawing:"
00347             link scheme is used for, so for now it is handled here.
00348 
00349             Also included here is a hack to allow MoinMoin macros. This routine
00350             checks for a link which starts with "<<". This link is passed to the
00351             MoinMoin formatter and the resulting markup is inserted into the
00352             document in the place of the original link reference.
00353         """
00354         if 'refuri' in node.attributes:
00355             refuri = node['refuri']
00356             prefix = ''
00357             link = refuri
00358             if ':' in refuri:
00359                 prefix, link = refuri.lstrip().split(':', 1)
00360 
00361             # First see if MoinMoin should handle completely. Exits through add_wiki_markup.
00362             if refuri.startswith('<<') and refuri.endswith('>>'): # moin macro
00363                 self.process_wiki_text(refuri)
00364                 self.wiki_text = self.fixup_wiki_formatting(self.wiki_text)
00365                 self.add_wiki_markup()
00366 
00367             if prefix == 'drawing':
00368                 self.process_wiki_text("[[%s]]" % refuri)
00369                 self.wiki_text = self.fixup_wiki_formatting(self.wiki_text)
00370                 self.add_wiki_markup()
00371 
00372             # From here down, all links are handled by docutils (except
00373             # missing attachments), just fixup node['refuri'].
00374             if prefix == 'attachment':
00375                 if not AttachFile.exists(self.request, self.request.page.page_name, link):
00376                     # Attachment doesn't exist, give to MoinMoin to insert upload text.
00377                     self.process_wiki_text("[[%s]]" % refuri)
00378                     self.wiki_text = self.fixup_wiki_formatting(self.wiki_text)
00379                     self.add_wiki_markup()
00380                 # Attachment exists, just get a link to it.
00381                 node['refuri'] = AttachFile.getAttachUrl(self.request.page.page_name, link, self.request)
00382                 if not [i for i in node.children if i.__class__ == docutils.nodes.image]:
00383                     node['classes'].append(prefix)
00384             elif prefix == 'wiki':
00385                 wiki_name, page_name = wikiutil.split_interwiki(link)
00386                 wikitag, wikiurl, wikitail, err = wikiutil.resolve_interwiki(self.request, wiki_name, page_name)
00387                 wikiurl = wikiutil.mapURL(self.request, wikiurl)
00388                 node['refuri'] = wikiutil.join_wiki(wikiurl, wikitail)
00389                 # Only add additional class information if the reference does
00390                 # not have a child image (don't want to add additional markup
00391                 # for images with targets).
00392                 if not [i for i in node.children if i.__class__ == docutils.nodes.image]:
00393                     node['classes'].append('interwiki')
00394             elif prefix != '':
00395                 # Some link scheme (http, file, https, mailto, etc.), add class
00396                 # information if the reference doesn't have a child image (don't
00397                 # want additional markup for images with targets).
00398                 # Don't touch the refuri.
00399                 if not [i for i in node.children if i.__class__ == docutils.nodes.image]:
00400                     node['classes'].append(prefix)
00401             else:
00402                 # Default case - make a link to a wiki page.
00403                 pagename, anchor = wikiutil.split_anchor(refuri)
00404                 page = Page(self.request, wikiutil.AbsPageName(self.formatter.page.page_name, pagename))
00405                 node['refuri'] = page.url(self.request, anchor=anchor)
00406                 if not page.exists():
00407                     node['classes'].append('nonexistent')
00408         html4css1.HTMLTranslator.visit_reference(self, node)

Here is the call graph for this function:


Member Data Documentation

Definition at line 286 of file text_rst.py.

Definition at line 256 of file text_rst.py.

Definition at line 266 of file text_rst.py.

Definition at line 257 of file text_rst.py.

Definition at line 270 of file text_rst.py.

Definition at line 261 of file text_rst.py.


The documentation for this class was generated from the following file: