Utils

Misc utilities for tags.

disseminate.tags.utils.content_to_str(content, target='.txt', **kwargs)

Convert a tag or string to a string for the specified target.

This function is used to convert an element, which is either a string, tag or list of strings and tags, to a string.

Parameters
contentUnion[str, List[Union[str, list, Tag], Tag]

The element to convert into a string.

targetstr, optional

The target format of the string to return.

Returns
formatted_strstr

A string in the specified target format

disseminate.tags.utils.copy_tag(tag)

Create a copy of the given tag.

The tag, attributes and content are deep copies, and the context points to the same context as the given tag.

Parameters
tagUnion[str, list Tag]

The tag to copy.

Returns
tag_copyTag

The tag copy.

disseminate.tags.utils.format_content(content, format_func, **kwargs)

Format the content using the format_func.

Parameters
contentUnion[str, List[Union[str, list, Tag]], optional

The content to format

format_funcstr

The name of the format_func to use. ex: ‘tex_fmt’

Returns
contentstr or list

The content formatted using the specified format_func.

disseminate.tags.utils.percentage(value)

Given a strings (or floats or ints), convert into a float number between [0.0, 100.0].

Parameters
valueUnion[str, int, float]
Returns
percentageUnion[int, None]

The percentage from 0 to 100, or None if the value could not be converted.

Examples

>>> percentage('10.2%')
11
>>> percentage('10.2')
1020
>>> percentage('0.3%')
1
>>> percentage('0.3')
30
>>> percentage(30)
30
disseminate.tags.utils.repl_tags(element, tag_class, replacement)

Replace all instances of a tag class with a replacement string.

Parameters
elementUnion[str, list Tag]

The element to replace tags with a replacement string.

tag_classTag

A tag class or subclass to replace.

replacementstr

The string to replace the tag with.

disseminate.tags.utils.replace_context(tag, new_context)

Replace the context for the given tag (and all subtags) to the given new_context.

Parameters
tagTag

The tag to replace the context.

new_contextType[BaseContext]

The new context to replace.

disseminate.tags.utils.tex_percentwidth(attributes, target='.tex', use_positional=False)

Generates an tex width string based on the ‘width’ entry in the attributes.

Currently, this function works in halves (w50), thirds (w33, w66) and quarters (w25, w75)

Parameters
attributesattributes.Attributes

The attributes dict

targetOptional[str]

The target format to format the attribute dict for.

use_positionalOptional[bool]

Insert the entry as a positional attribute

Returns
attributes: attributes.Attributes

The same attributes dict with the width inserted as a StringPositionalArgument, if a width was found.

Examples

>>> tex_percentwidth('width=50%')
Attributes{'width': '0.49\\textwidth'}
>>> tex_percentwidth('width=3.2in')
Attributes{'width': '3.2in'}
>>> tex_percentwidth('width=50%', use_positional=True)
Attributes{'width': '50%', '0.49\\textwidth': <class '...StringPositionalValue'>}
disseminate.tags.utils.xhtml_percentwidth(attributes, target='.html')

Generates an (x)html class name based on the ‘width’ entry in the attributes.

Currently, this function works in halves (w50), thirds (w33, w66) and quarters (w25, w75)

Parameters
attributesattributes.Attributes

The attributes dict

targetOptional[str]

The target format to format the attribute dict for.

Returns
attributes: attributes.Attributes

The same attributes dict with the ‘class’ entry set to an html class for the percent width, if a width was found.

Examples

>>> xhtml_percentwidth('width=50%')
Attributes{'width': '50%', 'class': 'w50'}
>>> xhtml_percentwidth('width=0%')  # None returned
Attributes{'width': '0%'}