Ejemplo n.º 1
0
    def to_rst(self) -> str:
        sections_dict = {"_items": [], "_description": ""}
        for file_path, md in self.items.items():
            file_parts = file_path.split(os.path.sep)
            folders = file_parts[:-1]
            file_name = file_parts[-1]
            resource = LectureResource.from_metadata(md, url=file_path)
            this_section_dict = sections_dict
            for folder in folders:
                if folder not in this_section_dict:
                    this_section_dict[folder] = {"_items": [], "_description": ""}
                this_section_dict = this_section_dict[folder]
            if file_name == "README.rst":
                this_section_dict["_description"] = (
                    DOCSRC_STATIC_PATH / file_path
                ).read_text()
            else:
                this_section_dict["_items"].append(resource)
            # TODO [#20]: static resource sorting would be more efficient in a separate loop after all items are created
            this_section_dict["_items"].sort(
                key=lambda res: res.index if res.index is not None else -1
            )

        sections_dict = dict(
            sorted(
                sections_dict.items(),
                key=lambda tup: f"aaa{STATIC_CONTENT_ORDER.index(tup[0])}"
                if tup[0] in STATIC_CONTENT_ORDER
                else tup[0],
            )
        )

        for section_name, section in sections_dict.items():
            if section_name in ["_items", "_description"]:
                continue
            sorted_section = dict(
                sorted(
                    section.items(),
                    key=lambda tup: f"aaa{EXAMPLE_SECTION_ORDER.index(tup[0])}"
                    if tup[0] in EXAMPLE_SECTION_ORDER
                    else tup[0],
                )
            )
            sections_dict[section_name] = sorted_section

        out_str = _static_sections_rst(sections_dict, 3)
        return out_str
Ejemplo n.º 2
0
 def to_rst(self) -> str:
     out_str = ""
     items_categories: Dict[str, List[LectureResource]] = {
         name: [] for name in CONTENT_TYPE_CODES_TO_NAMES.values()
     }
     items_categories["Other"] = []
     for file_name, md in self.items.items():
         resource = LectureResource.from_metadata(md)
         if md.content_type_code:
             category = CONTENT_TYPE_CODES_TO_NAMES[md.content_type_code]
         else:
             category = "Other"
         items_categories[category].append(resource)
     for items in items_categories.values():
         items.sort(key=lambda res: res.index if res.index is not None else -1)
     for category, items in items_categories.items():
         if not items:
             continue
         out_str += header_rst(category, 3)
         for resource in items:
             out_str += resource.to_rst()
     return out_str