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.
- 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.
- tagUnion[str, list
- Returns
- tag_copy
Tag
The tag copy.
- 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’
- contentUnion[str, List[Union[str, list,
- 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_class
Tag
A tag class or subclass to replace.
- replacementstr
The string to replace the tag with.
- elementUnion[str, list
- disseminate.tags.utils.replace_context(tag, new_context)¶
Replace the context for the given tag (and all subtags) to the given new_context.
- Parameters
- tag
Tag
The tag to replace the context.
- new_context
Type[BaseContext]
The new context to replace.
- tag
- 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
- attributes
attributes.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
- attributes
- Returns
- attributes:
attributes.Attributes
The same attributes dict with the width inserted as a StringPositionalArgument, if a width was found.
- attributes:
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
- attributes
attributes.Attributes
The attributes dict
- targetOptional[str]
The target format to format the attribute dict for.
- attributes
- 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.
- attributes:
Examples
>>> xhtml_percentwidth('width=50%') Attributes{'width': '50%', 'class': 'w50'} >>> xhtml_percentwidth('width=0%') # None returned Attributes{'width': '0%'}