ext/rest.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 10 Jun 2010 13:56:52 +0200
branchstable
changeset 5710 1c17e47df974
parent 5424 8ecbcbff9777
child 6109 47d9c0e0f7b7
permissions -rw-r--r--
[req] fix format_time w/ 0:0 which is evaluated to False
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4771
diff changeset
     1
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4771
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4771
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4771
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4771
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4771
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4771
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4771
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4771
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4771
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4771
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4771
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4771
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4771
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4771
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4771
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
"""rest publishing functions
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
996
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    20
contains some functions and setup of docutils for cubicweb. Provides the
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    21
following ReST directives:
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    22
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    23
* `eid`, create link to entity in the repository by their eid
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    24
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    25
* `card`, create link to card entity in the repository by their wikiid
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    26
  (proposing to create it when the refered card doesn't exist yet)
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    27
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    28
* `winclude`, reference to a web documentation file (in wdoc/ directories)
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    29
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    30
* `sourcecode` (if pygments is installed), source code colorization
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
from cStringIO import StringIO
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
from itertools import chain
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
from logging import getLogger
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
from os.path import join
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
from docutils import statemachine, nodes, utils, io
4771
e27d23f875c6 custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4769
diff changeset
    41
from docutils.core import Publisher
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
from docutils.parsers.rst import Parser, states, directives
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
from docutils.parsers.rst.roles import register_canonical_role, set_classes
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2311
diff changeset
    45
from logilab.mtconverter import ESC_UCAR_TABLE, ESC_CAR_TABLE, xml_escape
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
2467
6983631f5d0d don't fail on unknown eid, simply issue a warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2312
diff changeset
    47
from cubicweb import UnknownEid
704
0c2c8f0a6ded new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    48
from cubicweb.ext.html4zope import Writer
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
# We provide our own parser as an attempt to get rid of
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
# state machine reinstanciation
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
import re
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
# compile states.Body patterns
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
for k, v in states.Body.patterns.items():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
    if isinstance(v, str):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
        states.Body.patterns[k] = re.compile(v)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
# register ReStructured Text mimetype / extensions
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
import mimetypes
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
mimetypes.add_type('text/rest', '.rest')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
mimetypes.add_type('text/rest', '.rst')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
LOGGER = getLogger('cubicweb.rest')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
def eid_reference_role(role, rawtext, text, lineno, inliner,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
                       options={}, content=[]):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
    try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    71
            eid_num, rest = text.split(u':', 1)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
        except:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    73
            eid_num, rest = text, '#'+text
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
        eid_num = int(eid_num)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
        if eid_num < 0:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
            raise ValueError
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
    except ValueError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    78
        msg = inliner.reporter.error(
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    79
            'EID number must be a positive number; "%s" is invalid.'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
            % text, line=lineno)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
        prb = inliner.problematic(rawtext, rawtext, msg)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
        return [prb], [msg]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
    # Base URL mainly used by inliner.pep_reference; so this is correct:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
    context = inliner.document.settings.context
2467
6983631f5d0d don't fail on unknown eid, simply issue a warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2312
diff changeset
    85
    try:
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2680
diff changeset
    86
        refedentity = context._cw.entity_from_eid(eid_num)
2467
6983631f5d0d don't fail on unknown eid, simply issue a warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2312
diff changeset
    87
    except UnknownEid:
6983631f5d0d don't fail on unknown eid, simply issue a warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2312
diff changeset
    88
        ref = '#'
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2680
diff changeset
    89
        rest += u' ' + context._cw._('(UNEXISTANT EID)')
2467
6983631f5d0d don't fail on unknown eid, simply issue a warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2312
diff changeset
    90
    else:
6983631f5d0d don't fail on unknown eid, simply issue a warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2312
diff changeset
    91
        ref = refedentity.absolute_url()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
    set_classes(options)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    93
    return [nodes.reference(rawtext, utils.unescape(rest), refuri=ref,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
                            **options)], []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    97
def winclude_directive(name, arguments, options, content, lineno,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    98
                       content_offset, block_text, state, state_machine):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
    """Include a reST file as part of the content of this reST file.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
    same as standard include directive but using config.locate_doc_resource to
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
    get actual file to include.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
    Most part of this implementation is copied from `include` directive defined
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
    in `docutils.parsers.rst.directives.misc`
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   106
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
    context = state.document.settings.context
4769
5b878b02b67b [rest] cleanup, avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   108
    cw = context._cw
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   109
    source = state_machine.input_lines.source(
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   110
        lineno - state_machine.input_offset - 1)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   111
    #source_dir = os.path.dirname(os.path.abspath(source))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   112
    fid = arguments[0]
4769
5b878b02b67b [rest] cleanup, avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   113
    for lang in chain((cw.lang, cw.vreg.property_value('ui.language')),
5b878b02b67b [rest] cleanup, avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   114
                      cw.vreg.config.available_languages()):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   115
        rid = '%s_%s.rst' % (fid, lang)
4769
5b878b02b67b [rest] cleanup, avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   116
        resourcedir = cw.vreg.config.locate_doc_file(rid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
        if resourcedir:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   118
            break
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   119
    else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
        severe = state_machine.reporter.severe(
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
              'Problems with "%s" directive path:\nno resource matching %s.'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
              % (name, fid),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   123
              nodes.literal_block(block_text, block_text), line=lineno)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
        return [severe]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   125
    path = join(resourcedir, rid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
    encoding = options.get('encoding', state.document.settings.input_encoding)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   127
    try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128
        state.document.settings.record_dependencies.add(path)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   129
        include_file = io.FileInput(
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
            source_path=path, encoding=encoding,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
            error_handler=state.document.settings.input_encoding_error_handler,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   132
            handle_io_errors=None)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   133
    except IOError, error:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   134
        severe = state_machine.reporter.severe(
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   135
              'Problems with "%s" directive path:\n%s: %s.'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   136
              % (name, error.__class__.__name__, error),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   137
              nodes.literal_block(block_text, block_text), line=lineno)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   138
        return [severe]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   139
    try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   140
        include_text = include_file.read()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   141
    except UnicodeError, error:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
        severe = state_machine.reporter.severe(
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   143
              'Problem with "%s" directive:\n%s: %s'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   144
              % (name, error.__class__.__name__, error),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   145
              nodes.literal_block(block_text, block_text), line=lineno)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   146
        return [severe]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   147
    if options.has_key('literal'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   148
        literal_block = nodes.literal_block(include_text, include_text,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   149
                                            source=path)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   150
        literal_block.line = 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   151
        return literal_block
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
    else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   153
        include_lines = statemachine.string2lines(include_text,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   154
                                                  convert_whitespace=1)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   155
        state_machine.insert_input(include_lines, path)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   156
        return []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   157
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   158
winclude_directive.arguments = (1, 0, 1)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   159
winclude_directive.options = {'literal': directives.flag,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   160
                              'encoding': directives.encoding}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   161
996
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   162
try:
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   163
    from pygments import highlight
4719
aaed3f813ef8 kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4483
diff changeset
   164
    from pygments.lexers import get_lexer_by_name
996
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   165
    from pygments.formatters import HtmlFormatter
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   166
except ImportError:
4118
8a9a00a9405c quick and dirty fix trying to avoid rest directive conflicts when using sphinx (which seems to import the code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2680
diff changeset
   167
    pygments_directive = None
996
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   168
else:
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   169
    _PYGMENTS_FORMATTER = HtmlFormatter()
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   170
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   171
    def pygments_directive(name, arguments, options, content, lineno,
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   172
                           content_offset, block_text, state, state_machine):
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   173
        try:
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   174
            lexer = get_lexer_by_name(arguments[0])
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   175
        except ValueError:
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   176
            # no lexer found
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   177
            lexer = get_lexer_by_name('text')
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   178
        parsed = highlight(u'\n'.join(content), lexer, _PYGMENTS_FORMATTER)
4483
918fd9931cb7 cleanup, don't fail if no context set on the sourcecode directive
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4334
diff changeset
   179
        # don't fail if no context set on the sourcecode directive
918fd9931cb7 cleanup, don't fail if no context set on the sourcecode directive
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4334
diff changeset
   180
        try:
918fd9931cb7 cleanup, don't fail if no context set on the sourcecode directive
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4334
diff changeset
   181
            context = state.document.settings.context
918fd9931cb7 cleanup, don't fail if no context set on the sourcecode directive
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4334
diff changeset
   182
            context._cw.add_css('pygments.css')
918fd9931cb7 cleanup, don't fail if no context set on the sourcecode directive
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4334
diff changeset
   183
        except AttributeError:
918fd9931cb7 cleanup, don't fail if no context set on the sourcecode directive
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4334
diff changeset
   184
            # used outside cubicweb
918fd9931cb7 cleanup, don't fail if no context set on the sourcecode directive
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4334
diff changeset
   185
            pass
996
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   186
        return [nodes.raw('', parsed, format='html')]
1447
a1ca676294f0 don't use a singleton rest parser which may leads to concurrency bugs
sylvain.thenault@logilab.fr
parents: 1323
diff changeset
   187
996
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   188
    pygments_directive.arguments = (1, 0, 1)
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   189
    pygments_directive.content = 1
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   190
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   191
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   192
class CubicWebReSTParser(Parser):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   193
    """The (customized) reStructuredText parser."""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   194
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   195
    def __init__(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   196
        self.initial_state = 'Body'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   197
        self.state_classes = states.state_classes
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   198
        self.inliner = states.Inliner()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
        self.statemachine = states.RSTStateMachine(
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   200
              state_classes=self.state_classes,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   201
              initial_state=self.initial_state,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   202
              debug=0)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   203
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   204
    def parse(self, inputstring, document):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   205
        """Parse `inputstring` and populate `document`, a document tree."""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   206
        self.setup_parse(inputstring, document)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   207
        inputlines = statemachine.string2lines(inputstring,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   208
                                               convert_whitespace=1)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   209
        self.statemachine.run(inputlines, document, inliner=self.inliner)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   210
        self.finish_parse()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   211
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   212
4771
e27d23f875c6 custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4769
diff changeset
   213
# XXX docutils keep a ref on context, can't find a correct way to remove it
e27d23f875c6 custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4769
diff changeset
   214
class CWReSTPublisher(Publisher):
e27d23f875c6 custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4769
diff changeset
   215
    def __init__(self, context, settings, **kwargs):
e27d23f875c6 custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4769
diff changeset
   216
        Publisher.__init__(self, **kwargs)
e27d23f875c6 custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4769
diff changeset
   217
        self.set_components('standalone', 'restructuredtext', 'pseudoxml')
e27d23f875c6 custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4769
diff changeset
   218
        self.process_programmatic_settings(None, settings, None)
e27d23f875c6 custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4769
diff changeset
   219
        self.settings.context = context
e27d23f875c6 custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4769
diff changeset
   220
e27d23f875c6 custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4769
diff changeset
   221
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   222
def rest_publish(context, data):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   223
    """publish a string formatted as ReStructured Text to HTML
1447
a1ca676294f0 don't use a singleton rest parser which may leads to concurrency bugs
sylvain.thenault@logilab.fr
parents: 1323
diff changeset
   224
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   225
    :type context: a cubicweb application object
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   226
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   227
    :type data: str
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   228
    :param data: some ReST text
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   229
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   230
    :rtype: unicode
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   231
    :return:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   232
      the data formatted as HTML or the original data if an error occured
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   233
    """
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2680
diff changeset
   234
    req = context._cw
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   235
    if isinstance(data, unicode):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   236
        encoding = 'unicode'
2311
f178182b1305 actually close #344401 by removing unprintable characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   237
        # remove unprintable characters unauthorized in xml
f178182b1305 actually close #344401 by removing unprintable characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   238
        data = data.translate(ESC_UCAR_TABLE)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   239
    else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   240
        encoding = req.encoding
2311
f178182b1305 actually close #344401 by removing unprintable characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   241
        # remove unprintable characters unauthorized in xml
f178182b1305 actually close #344401 by removing unprintable characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   242
        data = data.translate(ESC_CAR_TABLE)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   243
    settings = {'input_encoding': encoding, 'output_encoding': 'unicode',
4771
e27d23f875c6 custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4769
diff changeset
   244
                'warning_stream': StringIO(),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   245
                # dunno what's the max, severe is 4, and we never want a crash
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   246
                # (though try/except may be a better option...)
1447
a1ca676294f0 don't use a singleton rest parser which may leads to concurrency bugs
sylvain.thenault@logilab.fr
parents: 1323
diff changeset
   247
                'halt_level': 10,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   248
                }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   249
    if context:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   250
        if hasattr(req, 'url'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   251
            base_url = req.url()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   252
        elif hasattr(context, 'absolute_url'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   253
            base_url = context.absolute_url()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   254
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   255
            base_url = req.base_url()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   256
    else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   257
        base_url = None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   258
    try:
4771
e27d23f875c6 custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4769
diff changeset
   259
        pub = CWReSTPublisher(context, settings,
e27d23f875c6 custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4769
diff changeset
   260
                              parser=CubicWebReSTParser(),
e27d23f875c6 custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4769
diff changeset
   261
                              writer=Writer(base_url=base_url),
e27d23f875c6 custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4769
diff changeset
   262
                              source_class=io.StringInput,
e27d23f875c6 custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4769
diff changeset
   263
                              destination_class=io.StringOutput)
e27d23f875c6 custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4769
diff changeset
   264
        pub.set_source(data)
e27d23f875c6 custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4769
diff changeset
   265
        pub.set_destination()
e27d23f875c6 custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4769
diff changeset
   266
        res = pub.publish(enable_exit_status=None)
e27d23f875c6 custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4769
diff changeset
   267
        # necessary for proper garbage collection, else a ref is kept somewhere in docutils...
e27d23f875c6 custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4769
diff changeset
   268
        del pub.settings.context
e27d23f875c6 custom rest publisher instead of using rest_publish. Avoid to keep a ref on the latest context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4769
diff changeset
   269
        return res
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   270
    except Exception:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   271
        LOGGER.exception('error while publishing ReST text')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   272
        if not isinstance(data, unicode):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   273
            data = unicode(data, encoding, 'replace')
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2311
diff changeset
   274
        return xml_escape(req._('error while publishing ReST text')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   275
                           + '\n\n' + data)
4118
8a9a00a9405c quick and dirty fix trying to avoid rest directive conflicts when using sphinx (which seems to import the code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2680
diff changeset
   276
8a9a00a9405c quick and dirty fix trying to avoid rest directive conflicts when using sphinx (which seems to import the code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2680
diff changeset
   277
8a9a00a9405c quick and dirty fix trying to avoid rest directive conflicts when using sphinx (which seems to import the code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2680
diff changeset
   278
_INITIALIZED = False
8a9a00a9405c quick and dirty fix trying to avoid rest directive conflicts when using sphinx (which seems to import the code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2680
diff changeset
   279
def cw_rest_init():
8a9a00a9405c quick and dirty fix trying to avoid rest directive conflicts when using sphinx (which seems to import the code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2680
diff changeset
   280
    global _INITIALIZED
8a9a00a9405c quick and dirty fix trying to avoid rest directive conflicts when using sphinx (which seems to import the code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2680
diff changeset
   281
    if _INITIALIZED:
8a9a00a9405c quick and dirty fix trying to avoid rest directive conflicts when using sphinx (which seems to import the code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2680
diff changeset
   282
        return
8a9a00a9405c quick and dirty fix trying to avoid rest directive conflicts when using sphinx (which seems to import the code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2680
diff changeset
   283
    _INITIALIZED = True
8a9a00a9405c quick and dirty fix trying to avoid rest directive conflicts when using sphinx (which seems to import the code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2680
diff changeset
   284
    register_canonical_role('eid', eid_reference_role)
8a9a00a9405c quick and dirty fix trying to avoid rest directive conflicts when using sphinx (which seems to import the code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2680
diff changeset
   285
    directives.register_directive('winclude', winclude_directive)
8a9a00a9405c quick and dirty fix trying to avoid rest directive conflicts when using sphinx (which seems to import the code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2680
diff changeset
   286
    if pygments_directive is not None:
8a9a00a9405c quick and dirty fix trying to avoid rest directive conflicts when using sphinx (which seems to import the code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2680
diff changeset
   287
        directives.register_directive('sourcecode', pygments_directive)