diff --git a/scielo_classic_website/models/document.py b/scielo_classic_website/models/document.py index b31ac5a..403ff07 100644 --- a/scielo_classic_website/models/document.py +++ b/scielo_classic_website/models/document.py @@ -212,22 +212,18 @@ def fpage_seq(self): def elocation(self): return self.page.get("elocation") - def get_section(self, lang): - if not hasattr(self, "_sections") or not self._sections: - self._sections = {} - logging.info("issue %s" % type(self.issue)) - logging.info("self.section_code %s" % self.section_code) - - for item in self.issue.get_sections(self.section_code): - logging.info("lang %s" % item) - self._sections[item["language"]] = item - - logging.info("get_section...") - logging.info("self._sections %s " % self._sections) + def get_sections(self): + items = {} + for item in self.issue.get_sections(self.section_code): + items[item["language"]] = item + return items + + def get_section_title(self, lang): try: - return self._sections[lang]["text"] + section = self.get_sections(lang) or self.get_sections("en") + return section["text"] except KeyError: - return None + return f"{self.issue.get_sections(self.section_code)} {lang} {self.section_code}" def get_article_title(self, lang): if not hasattr(self, "_article_titles") or not self._article_titles: diff --git a/scielo_classic_website/models/issue.py b/scielo_classic_website/models/issue.py index 9fdb2ce..2f986bd 100644 --- a/scielo_classic_website/models/issue.py +++ b/scielo_classic_website/models/issue.py @@ -64,6 +64,13 @@ def issue_label(self): ) def get_sections(self, code): - for item in self.sections: - if item["code"] == code: - yield item + return self.sections_by_code.get(code) or [] + + @property + def sections_by_code(self): + if not hasattr(self, '_sections_by_code') or not self._sections_by_code: + self._sections_by_code = {} + for item in self.sections: + self._sections_by_code.setdefault(item["code"], []) + self._sections_by_code[item["code"]].append(item) + return self._sections_by_code diff --git a/scielo_classic_website/spsxml/sps_xml_article_meta.py b/scielo_classic_website/spsxml/sps_xml_article_meta.py index 01fd747..dce268e 100644 --- a/scielo_classic_website/spsxml/sps_xml_article_meta.py +++ b/scielo_classic_website/spsxml/sps_xml_article_meta.py @@ -127,7 +127,7 @@ def transform(self, data): subject_group.set("subj-group-type", "heading") subject = ET.Element("subject") - subject.text = raw.original_section + subject.text = raw.get_section_title(raw.original_language) subject_group.append(subject) diff --git a/scielo_classic_website/spsxml/sps_xml_body_pipes.py b/scielo_classic_website/spsxml/sps_xml_body_pipes.py index d78f354..598dd49 100644 --- a/scielo_classic_website/spsxml/sps_xml_body_pipes.py +++ b/scielo_classic_website/spsxml/sps_xml_body_pipes.py @@ -633,6 +633,7 @@ def transform(self, data): xpath = f".//span[@name='style_{style}']" for node in xml.xpath(xpath): node.tag = style + node.attrib.pop("name") _report(xml, func_name=type(self)) return data diff --git a/scielo_classic_website/spsxml/sps_xml_pipes.py b/scielo_classic_website/spsxml/sps_xml_pipes.py index 499fa11..c6611e2 100644 --- a/scielo_classic_website/spsxml/sps_xml_pipes.py +++ b/scielo_classic_website/spsxml/sps_xml_pipes.py @@ -78,6 +78,7 @@ def _process(document): XMLBackPipe(), XMLArticleMetaCitationsPipe(), XMLSubArticlePipe(), + XMLStylePipe(), XMLArticleMetaCountsPipe(), XMLNormalizeSpacePipe(), XMLClosePipe(), @@ -336,7 +337,7 @@ def transform(self, data): subjectgroup = ET.Element("subj-group") subjectgroup.set("subj-group-type", "heading") sbj = ET.Element("subject") - sbj.text = raw.get_section(language) + sbj.text = raw.get_section_title(language) subjectgroup.append(sbj) articlecategories.append(subjectgroup) frontstub.append(articlecategories) @@ -379,3 +380,15 @@ def transform(self, data): frontstub.append(kwd_group) subarticle.append(frontstub) return data + + +class XMLStylePipe(plumber.Pipe): + def transform(self, data): + raw, xml = data + for style in ("bold", "italic", "sup", "sub", "underline"): + xpath = f".//span[@name='style_{style}']" + for node in xml.xpath(xpath): + node.tag = style + node.attrib.pop("name") + _report(xml, func_name=type(self)) + return data