Skip to content

Commit

Permalink
Merge pull request #56 from robertatakenaka/toc_sections
Browse files Browse the repository at this point in the history
Ao gerar o XML a partir do HTML, corrige o título da seção do sumário (troca o 'code' pro 'title')
  • Loading branch information
robertatakenaka authored Jul 6, 2024
2 parents 19835ba + ca72787 commit 6701f7e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 19 deletions.
24 changes: 10 additions & 14 deletions scielo_classic_website/models/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 10 additions & 3 deletions scielo_classic_website/models/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion scielo_classic_website/spsxml/sps_xml_article_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions scielo_classic_website/spsxml/sps_xml_body_pipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 14 additions & 1 deletion scielo_classic_website/spsxml/sps_xml_pipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def _process(document):
XMLBackPipe(),
XMLArticleMetaCitationsPipe(),
XMLSubArticlePipe(),
XMLStylePipe(),
XMLArticleMetaCountsPipe(),
XMLNormalizeSpacePipe(),
XMLClosePipe(),
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

0 comments on commit 6701f7e

Please sign in to comment.