本文整理汇总了Python中typogrify.filters.typogrify函数的典型用法代码示例。如果您正苦于以下问题:Python typogrify函数的具体用法?Python typogrify怎么用?Python typogrify使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了typogrify函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: read_file
def read_file(filename, fmt=None, settings=None):
"""Return a reader object using the given format."""
if not fmt:
fmt = filename.split('.')[-1]
if fmt not in _EXTENSIONS:
raise TypeError('Pelican does not know how to parse %s' % filename)
reader = _EXTENSIONS[fmt](settings)
settings_key = '%s_EXTENSIONS' % fmt.upper()
if settings and settings_key in settings:
reader.extensions = settings[settings_key]
if not reader.enabled:
raise ValueError("Missing dependencies for %s" % fmt)
content, metadata = reader.read(filename)
# eventually filter the content with typogrify if asked so
if settings and settings['TYPOGRIFY']:
from typogrify.filters import typogrify
content = typogrify(content)
metadata['title'] = typogrify(metadata['title'])
return content, metadata
示例2: read_file
def read_file(path, fmt=None, settings=None):
"""Return a reader object using the given format."""
base, ext = os.path.splitext(os.path.basename(path))
if not fmt:
fmt = ext[1:]
if fmt not in EXTENSIONS:
raise TypeError('Pelican does not know how to parse {}'.format(path))
if settings is None:
settings = {}
reader = EXTENSIONS[fmt](settings)
settings_key = '%s_EXTENSIONS' % fmt.upper()
if settings and settings_key in settings:
reader.extensions = settings[settings_key]
if not reader.enabled:
raise ValueError("Missing dependencies for %s" % fmt)
metadata = parse_path_metadata(
path=path, settings=settings, process=reader.process_metadata)
content, reader_metadata = reader.read(path)
metadata.update(reader_metadata)
# eventually filter the content with typogrify if asked so
if content and settings and settings.get('TYPOGRIFY'):
from typogrify.filters import typogrify
content = typogrify(content)
metadata['title'] = typogrify(metadata['title'])
return content, metadata
示例3: typogrify_wrapper
def typogrify_wrapper(text):
"""Ensures ignore_tags feature is backward compatible"""
try:
return typogrify(
text,
self.settings['TYPOGRIFY_IGNORE_TAGS'])
except TypeError:
return typogrify(text)
示例4: _postprocess
def _postprocess(self):
if self.rc.get('typogrify', False):
if self.output:
self.output = typogrify(self.output)
if 'title' in self.meta:
self.meta['title'] = typogrify(self.meta['title'])
if 'tags' in self.meta:
self.meta['tags'] = [t.strip() for t in self.meta['tags'].split(',')]
示例5: read_file
def read_file(self, base_path, path, content_class=Page, fmt=None,
context=None, preread_signal=None, preread_sender=None,
context_signal=None, context_sender=None):
"""Return a content object parsed with the given format."""
path = os.path.abspath(os.path.join(base_path, path))
source_path = os.path.relpath(path, base_path)
logger.debug('read file {} -> {}'.format(
source_path, content_class.__name__))
if not fmt:
_, ext = os.path.splitext(os.path.basename(path))
fmt = ext[1:]
if fmt not in self.readers:
raise TypeError(
'Pelican does not know how to parse {}'.format(path))
if preread_signal:
logger.debug('signal {}.send({})'.format(
preread_signal, preread_sender))
preread_signal.send(preread_sender)
reader = self.readers[fmt]
metadata = default_metadata(
settings=self.settings, process=reader.process_metadata)
metadata.update(path_metadata(
full_path=path, source_path=source_path,
settings=self.settings))
metadata.update(parse_path_metadata(
source_path=source_path, settings=self.settings,
process=reader.process_metadata))
content, reader_metadata = reader.read(path)
metadata.update(reader_metadata)
if content:
# find images with empty alt
find_empty_alt(content, path)
# eventually filter the content with typogrify if asked so
if self.settings['TYPOGRIFY']:
from typogrify.filters import typogrify
if content:
content = typogrify(content)
metadata['title'] = typogrify(metadata['title'])
if 'summary' in metadata:
metadata['summary'] = typogrify(metadata['summary'])
if context_signal:
logger.debug('signal {}.send({}, <metadata>)'.format(
context_signal, context_sender))
context_signal.send(context_sender, metadata=metadata)
return content_class(content=content, metadata=metadata,
settings=self.settings, source_path=path,
context=context)
示例6: read_file
def read_file(
base_path,
path,
content_class=Page,
fmt=None,
settings=None,
context=None,
preread_signal=None,
preread_sender=None,
context_signal=None,
context_sender=None,
):
"""Return a content object parsed with the given format."""
path = os.path.abspath(os.path.join(base_path, path))
source_path = os.path.relpath(path, base_path)
base, ext = os.path.splitext(os.path.basename(path))
logger.debug("read file {} -> {}".format(source_path, content_class.__name__))
if not fmt:
fmt = ext[1:]
if fmt not in EXTENSIONS:
raise TypeError("Pelican does not know how to parse {}".format(path))
if preread_signal:
logger.debug("signal {}.send({})".format(preread_signal, preread_sender))
preread_signal.send(preread_sender)
if settings is None:
settings = {}
reader_class = EXTENSIONS[fmt]
if not reader_class.enabled:
raise ValueError("Missing dependencies for {}".format(fmt))
reader = reader_class(settings)
settings_key = "%s_EXTENSIONS" % fmt.upper()
if settings and settings_key in settings:
reader.extensions = settings[settings_key]
metadata = default_metadata(settings=settings, process=reader.process_metadata)
metadata.update(path_metadata(full_path=path, source_path=source_path, settings=settings))
metadata.update(parse_path_metadata(source_path=source_path, settings=settings, process=reader.process_metadata))
content, reader_metadata = reader.read(path)
metadata.update(reader_metadata)
# eventually filter the content with typogrify if asked so
if content and settings and settings["TYPOGRIFY"]:
from typogrify.filters import typogrify
content = typogrify(content)
metadata["title"] = typogrify(metadata["title"])
if context_signal:
logger.debug("signal {}.send({}, <metadata>)".format(context_signal, context_sender))
context_signal.send(context_sender, metadata=metadata)
return content_class(content=content, metadata=metadata, settings=settings, source_path=path, context=context)
示例7: typogrify
def typogrify(self):
"""filter the content with typogrify"""
if self._settings.get('TYPOGRIFY', False):
from typogrify.filters import typogrify
if self._content:
self._content = typogrify(self._content)
self._metadata['title'] = typogrify(self._metadata['title'])
if 'summary' in self._metadata:
self._metadata['summary'] = typogrify(self._metadata['summary'])
示例8: process_content
def process_content(instance):
"""Processes content, with logic to ensure that typogrify does not clash
with math.
In addition, mathjax script is inserted at the end of the content thereby
making it independent of the template
"""
if not instance._content:
return
ignore_within = ignore_content(instance._content)
if _WRAP_LATEX:
instance._content, math = wrap_math(instance._content, ignore_within)
else:
math = True if _MATH_REGEX.search(instance._content) else False
# The user initially set typogrify to be True, but since it would clash
# with math, we set it to False. This means that the default reader will
# not call typogrify, so it is called here, where we are able to control
# logic for it ignore math if necessary
if _TYPOGRIFY:
# Tell typogrify to ignore the tags that math has been wrapped in
# also, typogrify must always ignore mml (math) tags
ignore_tags = [_WRAP_LATEX,'math'] if _WRAP_LATEX else ['math']
# Exact copy of the logic as found in the default reader
from typogrify.filters import typogrify
instance._content = typogrify(instance._content, ignore_tags)
instance.metadata['title'] = typogrify(instance.metadata['title'], ignore_tags)
if math:
if _MATHJAX_SETTINGS['auto_insert']:
# Mathjax script added to content automatically. Now it
# does not need to be explicitly added to the template
instance._content += _MATHJAX_SCRIPT.format(**_MATHJAX_SETTINGS)
else:
# Place the burden on ensuring mathjax script is available to
# browser on the template designer (see README for more details)
instance.mathjax = True
# The summary needs special care because math math cannot just be cut
# off
summary = process_summary(instance, ignore_within)
if summary != None:
instance._summary = summary
示例9: markup
def markup(text):
"""
Mark up plain text into fancy HTML.
"""
return typogrify(
markdown(text,
lazy_ol=False,
output_format='html5',
extensions=['abbr', 'codehilite', 'fenced_code', 'sane_lists',
'smart_strong']))
示例10: process_content
def process_content(instance):
"""Processes content, with logic to ensure that typogrify does not clash
with latex.
In addition, mathjax script is inserted at the end of the content thereby
making it independent of the template
"""
if not instance._content:
return
ignore_within = ignore_content(instance._content)
if _WRAP_TAG:
instance._content, latex = wrap_latex(instance._content, ignore_within)
else:
latex = True if _LATEX_REGEX.search(instance._content) else False
# The user initially set typogrify to be True, but since it would clash
# with latex, we set it to False. This means that the default reader will
# not call typogrify, so it is called here, where we are able to control
# logic for it ignore latex if necessary
if _TYPOGRIFY:
# Tell typogrify to ignore the tags that latex has been wrapped in
# also, typogrify must always ignore mml (math) tags
ignore_tags = [_WRAP_TAG,'math'] if _WRAP_TAG else ['math']
# Exact copy of the logic as found in the default reader
from typogrify.filters import typogrify
instance._content = typogrify(instance._content, ignore_tags)
instance.metadata['title'] = typogrify(instance.metadata['title'], ignore_tags)
if latex:
# Mathjax script added to the end of article. Now it does not need to
# be explicitly added to the template
instance._content += _MATHJAX_SCRIPT.format(**_MATHJAX_SETTINGS)
# The summary needs special care because latex math cannot just be cut
# off
summary = process_summary(instance, ignore_within)
if summary != None:
instance._summary = summary
示例11: present_story_so_far
def present_story_so_far(self):
if not self.state.messages:
self.state = self.state.render_situation()
story_text = u'\n\n'.join(m for m in self.state.messages
if m is not None and m.strip())
logger.debug("#### The Story:\n%s", story_text)
md_extensions = self.plot.config['markdown']['extensions']
story_text = markdown.markdown(story_text, extensions=md_extensions)
story = typogrify(story_text)
return story
示例12: read_file
def read_file(filename, fmt=None, settings=None):
"""Return a reader object using the given format."""
base, ext = os.path.splitext(os.path.basename(filename))
if not fmt:
fmt = ext[1:]
if fmt not in _EXTENSIONS:
raise TypeError('Pelican does not know how to parse %s' % filename)
reader = _EXTENSIONS[fmt](settings)
settings_key = '%s_EXTENSIONS' % fmt.upper()
if settings and settings_key in settings:
reader.extensions = settings[settings_key]
if not reader.enabled:
raise ValueError("Missing dependencies for %s" % fmt)
content, metadata = reader.read(filename)
# eventually filter the content with typogrify if asked so
if settings and settings.get('TYPOGRIFY'):
from typogrify.filters import typogrify
content = typogrify(content)
metadata['title'] = typogrify(metadata['title'])
filename_metadata = settings and settings.get('FILENAME_METADATA')
if filename_metadata:
match = re.match(filename_metadata, base)
if match:
# .items() for py3k compat.
for k, v in match.groupdict().items():
if k not in metadata:
k = k.lower() # metadata must be lowercase
metadata[k] = reader.process_metadata(k, v)
return content, metadata
示例13: render_files
def render_files(settings, template, context={}):
env = Environment(loader=FileSystemLoader(os.path.dirname(__file__)))
if 'context' in settings:
settings['context'].update(context)
else:
settings['context'] = context
full_context = {k.upper(): v for k, v in settings['context'].items()}
try:
used_template = template if template else settings['template']
except KeyError:
print('Some error shit') # TODO: Logging
sys.exit()
html_output = env.get_template(settings['template']).render(**full_context)
text_output = fromstring(html_output).text_content()
typo_output = typogrify(html_output)
if settings.get('html_output'):
with open(settings['html_output'], 'w') as html:
html.write(typo_output)
if settings.get('text_output'):
with open(settings['text_output'], 'w') as text:
text.write(text_output)
else:
print(text_output)
示例14: render_files
def render_files(settings, template, context={}):
try:
used_template = template if template else settings['template']
except KeyError:
LOGGER.error('No template defined')
sys.exit()
env = Environment(loader=FileSystemLoader(os.getcwd()))
if 'context' in settings:
settings['context'].update(context)
else:
settings['context'] = context
full_context = {k.upper(): v for k, v in settings['context'].items()}
html_output = env.get_template(settings['template']).render(**full_context)
text_output = fromstring(html_output).text_content()
typo_output = typogrify(html_output)
output_files = settings['output']
if output_files.get('html'):
with open(output_files.get('html'), 'w') as html:
html.write(typo_output)
if output_files.get('text'):
with open(output_files.get('text'), 'w') as text:
text.write(text_output)
else:
print(text_output)
示例15: typogrify
def typogrify(s):
return jinja2_filters.typogrify(s)