ext/rest.py
author Pierre-Yves David <pierre-yves.david@logilab.fr>
Tue, 20 Mar 2012 18:28:24 +0100
changeset 8324 75694a61f089
parent 7879 9aae456abab5
child 8695 358d8bed9626
child 8710 becbbbc840b8
permissions -rw-r--r--
[login redirect] only add postlogin_path argument when meaningful
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7815
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7732
diff changeset
     1
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
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
     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
6938
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6275
diff changeset
    50
from cubicweb.web.views import vid_from_rset # XXX better not to import c.w.views here...
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6275
diff changeset
    51
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
# We provide our own parser as an attempt to get rid of
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
# state machine reinstanciation
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
import re
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
# compile states.Body patterns
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
for k, v in states.Body.patterns.items():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
    if isinstance(v, str):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
        states.Body.patterns[k] = re.compile(v)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
# register ReStructured Text mimetype / extensions
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
import mimetypes
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
mimetypes.add_type('text/rest', '.rest')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
mimetypes.add_type('text/rest', '.rst')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
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
LOGGER = getLogger('cubicweb.rest')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
def eid_reference_role(role, rawtext, text, lineno, inliner,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
                       options={}, content=[]):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    71
    try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    73
            eid_num, rest = text.split(u':', 1)
7815
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7732
diff changeset
    74
        except ValueError:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
            eid_num, rest = text, '#'+text
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
        eid_num = int(eid_num)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
        if eid_num < 0:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    78
            raise ValueError
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    79
    except ValueError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
        msg = inliner.reporter.error(
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
            'EID number must be a positive number; "%s" is invalid.'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
            % text, line=lineno)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
        prb = inliner.problematic(rawtext, rawtext, msg)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
        return [prb], [msg]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
    # Base URL mainly used by inliner.pep_reference; so this is correct:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
    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
    87
    try:
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2680
diff changeset
    88
        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
    89
    except UnknownEid:
6983631f5d0d don't fail on unknown eid, simply issue a warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2312
diff changeset
    90
        ref = '#'
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2680
diff changeset
    91
        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
    92
    else:
6983631f5d0d don't fail on unknown eid, simply issue a warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2312
diff changeset
    93
        ref = refedentity.absolute_url()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
    set_classes(options)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
    return [nodes.reference(rawtext, utils.unescape(rest), refuri=ref,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
                            **options)], []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    97
6938
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6275
diff changeset
    98
def rql_role(role, rawtext, text, lineno, inliner, options={}, content=[]):
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6275
diff changeset
    99
    """:rql:`Any X,Y WHERE X is CWUser, X login Y:table`"""
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6275
diff changeset
   100
    _cw = inliner.document.settings.context._cw
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6275
diff changeset
   101
    text = text.strip()
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6275
diff changeset
   102
    if ':' in text:
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6275
diff changeset
   103
        rql, vid = text.rsplit(u':', 1)
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6275
diff changeset
   104
        rql = rql.strip()
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6275
diff changeset
   105
    else:
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6275
diff changeset
   106
        rql, vid = text, None
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6275
diff changeset
   107
    _cw.ensure_ro_rql(rql)
7732
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   108
    try:
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   109
        rset = _cw.execute(rql, {'userid': _cw.user.eid})
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   110
        if rset:
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   111
            if vid is None:
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   112
                vid = vid_from_rset(_cw, rset, _cw.vreg.schema)
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   113
        else:
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   114
            vid = 'noresult'
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   115
        view = _cw.vreg['views'].select(vid, _cw, rset=rset)
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   116
        content = view.render()
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   117
    except Exception, exc:
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7577
diff changeset
   118
        content = 'an error occured while interpreting this rql directive: %r' % exc
6938
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6275
diff changeset
   119
    set_classes(options)
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6275
diff changeset
   120
    return [nodes.raw('', content, format='html')], []
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
def winclude_directive(name, arguments, options, content, lineno,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   123
                       content_offset, block_text, state, state_machine):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
    """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
   125
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
    same as standard include directive but using config.locate_doc_resource to
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   127
    get actual file to include.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   129
    Most part of this implementation is copied from `include` directive defined
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
    in `docutils.parsers.rst.directives.misc`
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   132
    context = state.document.settings.context
4769
5b878b02b67b [rest] cleanup, avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   133
    cw = context._cw
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   134
    source = state_machine.input_lines.source(
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   135
        lineno - state_machine.input_offset - 1)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   136
    #source_dir = os.path.dirname(os.path.abspath(source))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   137
    fid = arguments[0]
4769
5b878b02b67b [rest] cleanup, avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   138
    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
   139
                      cw.vreg.config.available_languages()):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   140
        rid = '%s_%s.rst' % (fid, lang)
4769
5b878b02b67b [rest] cleanup, avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   141
        resourcedir = cw.vreg.config.locate_doc_file(rid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
        if resourcedir:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   143
            break
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   144
    else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   145
        severe = state_machine.reporter.severe(
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   146
              'Problems with "%s" directive path:\nno resource matching %s.'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   147
              % (name, fid),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   148
              nodes.literal_block(block_text, block_text), line=lineno)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   149
        return [severe]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   150
    path = join(resourcedir, rid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   151
    encoding = options.get('encoding', state.document.settings.input_encoding)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
    try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   153
        state.document.settings.record_dependencies.add(path)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   154
        include_file = io.FileInput(
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   155
            source_path=path, encoding=encoding,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   156
            error_handler=state.document.settings.input_encoding_error_handler,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   157
            handle_io_errors=None)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   158
    except IOError, error:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   159
        severe = state_machine.reporter.severe(
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   160
              'Problems with "%s" directive path:\n%s: %s.'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   161
              % (name, error.__class__.__name__, error),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   162
              nodes.literal_block(block_text, block_text), line=lineno)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   163
        return [severe]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   164
    try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   165
        include_text = include_file.read()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   166
    except UnicodeError, error:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   167
        severe = state_machine.reporter.severe(
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   168
              'Problem with "%s" directive:\n%s: %s'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   169
              % (name, error.__class__.__name__, error),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   170
              nodes.literal_block(block_text, block_text), line=lineno)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   171
        return [severe]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   172
    if options.has_key('literal'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   173
        literal_block = nodes.literal_block(include_text, include_text,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   174
                                            source=path)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   175
        literal_block.line = 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   176
        return literal_block
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   177
    else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   178
        include_lines = statemachine.string2lines(include_text,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   179
                                                  convert_whitespace=1)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   180
        state_machine.insert_input(include_lines, path)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   181
        return []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   182
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   183
winclude_directive.arguments = (1, 0, 1)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   184
winclude_directive.options = {'literal': directives.flag,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   185
                              'encoding': directives.encoding}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   186
996
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   187
try:
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   188
    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
   189
    from pygments.lexers import get_lexer_by_name
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   190
    from pygments.formatters.html import HtmlFormatter
996
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   191
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
   192
    pygments_directive = None
996
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   193
else:
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   194
    _PYGMENTS_FORMATTER = HtmlFormatter()
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   195
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   196
    def pygments_directive(name, arguments, options, content, lineno,
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   197
                           content_offset, block_text, state, state_machine):
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   198
        try:
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   199
            lexer = get_lexer_by_name(arguments[0])
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   200
        except ValueError:
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   201
            # no lexer found
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   202
            lexer = get_lexer_by_name('text')
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   203
        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
   204
        # 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
   205
        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
   206
            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
   207
            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
   208
        except AttributeError:
7577
9892937d9041 [all] remove pattern "try: function() except AttributeError: pass" (closes #1787966)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6938
diff changeset
   209
            # used outside cubicweb XXX use hasattr instead
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
   210
            pass
996
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   211
        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
   212
996
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   213
    pygments_directive.arguments = (1, 0, 1)
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   214
    pygments_directive.content = 1
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   215
9495a0594b95 sourcecode directive
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   216
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   217
class CubicWebReSTParser(Parser):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   218
    """The (customized) reStructuredText parser."""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   219
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   220
    def __init__(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   221
        self.initial_state = 'Body'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   222
        self.state_classes = states.state_classes
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   223
        self.inliner = states.Inliner()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   224
        self.statemachine = states.RSTStateMachine(
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   225
              state_classes=self.state_classes,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   226
              initial_state=self.initial_state,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   227
              debug=0)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   228
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   229
    def parse(self, inputstring, document):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   230
        """Parse `inputstring` and populate `document`, a document tree."""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   231
        self.setup_parse(inputstring, document)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   232
        inputlines = statemachine.string2lines(inputstring,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   233
                                               convert_whitespace=1)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   234
        self.statemachine.run(inputlines, document, inliner=self.inliner)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   235
        self.finish_parse()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   236
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   237
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
   238
# 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
   239
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
   240
    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
   241
        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
   242
        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
   243
        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
   244
        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
   245
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
   246
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   247
def rest_publish(context, data):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   248
    """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
   249
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   250
    :type context: a cubicweb application object
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   251
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   252
    :type data: str
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   253
    :param data: some ReST text
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   254
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   255
    :rtype: unicode
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   256
    :return:
6109
47d9c0e0f7b7 integrate Celso's work on translation file: proper/complete spanish translation, fixed some typos in french translation, occured -> occurred fix in various places
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   257
      the data formatted as HTML or the original data if an error occurred
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   258
    """
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2680
diff changeset
   259
    req = context._cw
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   260
    if isinstance(data, unicode):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   261
        encoding = 'unicode'
2311
f178182b1305 actually close #344401 by removing unprintable characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   262
        # 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
   263
        data = data.translate(ESC_UCAR_TABLE)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   264
    else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   265
        encoding = req.encoding
2311
f178182b1305 actually close #344401 by removing unprintable characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   266
        # 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
   267
        data = data.translate(ESC_CAR_TABLE)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   268
    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
   269
                'warning_stream': StringIO(),
6275
20f30e01ae59 [ReST] settings update: add traceback=True to avoid sys.exit, and set stylesheet to None since we don't care about it and that may makes docutils stupidly fail according to the cwd at import time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6109
diff changeset
   270
                'traceback': True, # don't sys.exit
20f30e01ae59 [ReST] settings update: add traceback=True to avoid sys.exit, and set stylesheet to None since we don't care about it and that may makes docutils stupidly fail according to the cwd at import time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6109
diff changeset
   271
                'stylesheet': None, # don't try to embed stylesheet (may cause
20f30e01ae59 [ReST] settings update: add traceback=True to avoid sys.exit, and set stylesheet to None since we don't care about it and that may makes docutils stupidly fail according to the cwd at import time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6109
diff changeset
   272
                                    # obscure bug due to docutils computing
20f30e01ae59 [ReST] settings update: add traceback=True to avoid sys.exit, and set stylesheet to None since we don't care about it and that may makes docutils stupidly fail according to the cwd at import time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6109
diff changeset
   273
                                    # relative path according to the directory
20f30e01ae59 [ReST] settings update: add traceback=True to avoid sys.exit, and set stylesheet to None since we don't care about it and that may makes docutils stupidly fail according to the cwd at import time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6109
diff changeset
   274
                                    # used *at import time*
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   275
                # dunno what's the max, severe is 4, and we never want a crash
6275
20f30e01ae59 [ReST] settings update: add traceback=True to avoid sys.exit, and set stylesheet to None since we don't care about it and that may makes docutils stupidly fail according to the cwd at import time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6109
diff changeset
   276
                # (though try/except may be a better option...). May be the
20f30e01ae59 [ReST] settings update: add traceback=True to avoid sys.exit, and set stylesheet to None since we don't care about it and that may makes docutils stupidly fail according to the cwd at import time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6109
diff changeset
   277
                # above traceback option will avoid this?
1447
a1ca676294f0 don't use a singleton rest parser which may leads to concurrency bugs
sylvain.thenault@logilab.fr
parents: 1323
diff changeset
   278
                'halt_level': 10,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   279
                }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   280
    if context:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   281
        if hasattr(req, 'url'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   282
            base_url = req.url()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   283
        elif hasattr(context, 'absolute_url'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   284
            base_url = context.absolute_url()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   285
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   286
            base_url = req.base_url()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   287
    else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   288
        base_url = None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   289
    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
   290
        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
   291
                              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
   292
                              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
   293
                              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
   294
                              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
   295
        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
   296
        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
   297
        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
   298
        # 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
   299
        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
   300
        return res
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   301
    except Exception:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   302
        LOGGER.exception('error while publishing ReST text')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   303
        if not isinstance(data, unicode):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   304
            data = unicode(data, encoding, 'replace')
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2311
diff changeset
   305
        return xml_escape(req._('error while publishing ReST text')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   306
                           + '\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
   307
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
   308
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
   309
_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
   310
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
   311
    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
   312
    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
   313
        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
   314
    _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
   315
    register_canonical_role('eid', eid_reference_role)
6938
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6275
diff changeset
   316
    register_canonical_role('rql', rql_role)
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
   317
    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
   318
    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
   319
        directives.register_directive('sourcecode', pygments_directive)