Projet

Général

Profil

0001-misc-move-RTF-escape-code-to-ezt-processor-25521.patch

Frédéric Péters, 28 juillet 2018 15:05

Télécharger (3,3 ko)

Voir les différences:

Subject: [PATCH 1/3] misc: move RTF escape code to ezt processor (#25521)

 wcs/qommon/ezt.py         | 15 +++++++++++++++
 wcs/wf/export_to_model.py | 22 ++--------------------
 2 files changed, 17 insertions(+), 20 deletions(-)
wcs/qommon/ezt.py
240 240
FORMAT_RAW = 'raw'
241 241
FORMAT_HTML = 'html'
242 242
FORMAT_XML = 'xml'
243
FORMAT_RTF = 'rtf'
243 244

  
244 245
#
245 246
# This regular expression matches three alternatives:
......
283 284
    FORMAT_RAW  : '_cmd_print',
284 285
    FORMAT_HTML : '_cmd_print_html',
285 286
    FORMAT_XML  : '_cmd_print_xml',
287
    FORMAT_RTF  : '_cmd_print_rtf',
286 288
    }
287 289

  
288 290
  def __init__(self, fname=None, compress_whitespace=1,
......
473 475
  def _cmd_print_html(self, valref, fp, ctx):
474 476
    _write_value(valref, fp, ctx, cgi.escape)
475 477

  
478
  def _cmd_print_rtf(self, valref, fp, ctx):
479
    def char2rtf(c):
480
      if ord(c) < 128:
481
        return c
482
      else:
483
        return '\\u%d?' % ord(c)
484

  
485
    def rtf_escape(s):
486
      s = ''.join([char2rtf(c) for c in s])
487
      return '{\\uc1{%s}}' % s
488

  
489
    _write_value(valref, fp, ctx, rtf_escape)
490

  
476 491
  def _cmd_print_xml(self, valref, fp, ctx):
477 492
    ### use the same quoting as HTML for now
478 493
    self._cmd_print_html(valref, fp, ctx)
wcs/wf/export_to_model.py
28 28
from quixote.directory import Directory
29 29
from quixote.html import htmltext
30 30

  
31
from qommon import _
31
from qommon import _, ezt
32 32
from qommon import get_logger
33 33
from qommon.form import (SingleSelectWidget, WidgetList, CheckboxWidget,
34 34
                         StringWidget, UploadWidget, WysiwygTextWidget, Upload,
......
175 175
        return self.wfstatusitem.apply_template_to_formdata(self.formdata).read()
176 176

  
177 177

  
178
def char2rtf(c):
179
    if ord(c) < 128:
180
        return c
181
    else:
182
        return '\\u%d?' % ord(c)
183

  
184

  
185
def str2rtf(s):
186
    s = ''.join([char2rtf(c) for c in s])
187
    return '{\\uc1{%s}}' % s
188

  
189

  
190
def rtf_process(value):
191
    if value is None:
192
        return None
193
    return str2rtf(unicode(str(value), get_publisher().site_charset))
194

  
195

  
196 178
class ExportToModel(WorkflowStatusItem):
197 179
    description = N_('Document Creation')
198 180
    key = 'export_to_model'
......
421 403
            # force ezt_only=True because an RTF file may contain {{ characters
422 404
            # and would be seen as a Django template
423 405
            return StringIO(template_on_formdata(formdata, self.model_file.get_file().read(),
424
                                                 process=rtf_process, ezt_only=True))
406
                                                 ezt_format=ezt.FORMAT_RTF, ezt_only=True))
425 407
        except TemplateError as e:
426 408
            url = formdata.get_url()
427 409
            get_logger().error('error in template for export to model [%s]: %s' % (url, str(e)))
428
-