Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correct handling of table caption elements in panflute 2.x #148

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
110 changes: 59 additions & 51 deletions ipypublish/filters_pandoc/prepare_cites.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
meta["ipub"]["pandoc"]["at_notation"] = False

"""
from panflute import Element, Doc, Cite, RawInline, Link # noqa: F401
from panflute import Element, Doc, Cite, RawInline, Link, Caption # noqa: F401
import panflute as pf


Expand All @@ -57,57 +57,65 @@ def process_citations(element, doc):
if not initial_content:
return None

final_content = []
skip = 0
for subel in initial_content:

if skip:
skip -= 1
continue

if not isinstance(subel, pf.Cite):
final_content.append(subel)
continue

classes = []
attributes = {}
append = None

# check if the cite has a valid prefix, if so extract it
if (
isinstance(subel.prev, pf.Str)
and subel.prev.text
and (subel.prev.text[-1] in dict(PREFIX_MAP))
):

prefix = subel.prev.text[-1]
mapping = dict(dict(PREFIX_MAP)[prefix])
classes.extend(mapping["classes"])
attributes.update(mapping["attributes"])

# remove prefix from preceding string
string = final_content.pop()
if len(string.text) > 1:
final_content.append(pf.Str(string.text[:-1]))

# check if the cite has a preceding class/attribute container
attr_dict = find_attributes(subel, allow_space=True)
if attr_dict:
classes.extend(attr_dict["classes"])
attributes.update(attr_dict["attributes"])
skip = len(attr_dict["elements"])
append = attr_dict["append"]

if classes or attributes:
classes.append(ATTRIBUTE_CITE_CLASS)
final_content.append(
pf.Span(subel, classes=sorted(set(classes)), attributes=attributes)
)
if isinstance(initial_content, Caption):
processed_caption = process_citations(initial_content.content, doc)
processed_short_caption = process_citations(initial_content.short_caption, doc)
if processed_caption is None:
final_content = Caption()
else:
final_content.append(subel)

if append:
final_content.append(append)
final_content = Caption(processed_caption, processed_short_caption)
else:
final_content = []
skip = 0
for subel in initial_content:

if skip:
skip -= 1
continue

if not isinstance(subel, pf.Cite):
final_content.append(subel)
continue

classes = []
attributes = {}
append = None

# check if the cite has a valid prefix, if so extract it
if (
isinstance(subel.prev, pf.Str)
and subel.prev.text
and (subel.prev.text[-1] in dict(PREFIX_MAP))
):

prefix = subel.prev.text[-1]
mapping = dict(dict(PREFIX_MAP)[prefix])
classes.extend(mapping["classes"])
attributes.update(mapping["attributes"])

# remove prefix from preceding string
string = final_content.pop()
if len(string.text) > 1:
final_content.append(pf.Str(string.text[:-1]))

# check if the cite has a preceding class/attribute container
attr_dict = find_attributes(subel, allow_space=True)
if attr_dict:
classes.extend(attr_dict["classes"])
attributes.update(attr_dict["attributes"])
skip = len(attr_dict["elements"])
append = attr_dict["append"]

if classes or attributes:
classes.append(ATTRIBUTE_CITE_CLASS)
final_content.append(
pf.Span(subel, classes=sorted(set(classes)), attributes=attributes)
)
else:
final_content.append(subel)

if append:
final_content.append(append)

setattr(element, content_attr, final_content)
return element
Expand Down
7 changes: 4 additions & 3 deletions ipypublish/filters_pandoc/prepare_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ def resolve_tables(element, doc):
if element.caption: # type: Inline
# attributes = _find_attribute(element.caption[0],
# allow_any=True, delete_preceding=False)
attributes = find_attributes(
element.caption[-1], search_left=True, include_element=True
)
# attributes = find_attributes(
# element.caption[-1], search_left=True, include_element=True
# )
atributes = element.caption.short_caption

if not attributes:
return None
Expand Down