def to_rst(self) -> str: out_str = header_rst(self.title, 2) out_str += f'\n{self.description}\n' if self.show_aggregate_resources and self.resources: out_str += (header_rst('Resources', 3) + "\n" + "\n".join([res.to_rst() for res in self.resources]) + "\n") for lecture in self: out_str += lecture.to_rst() return out_str
def _description_rst(self) -> str: out_str = '' if self.exercises is not None: num_exercises = len(self.exercises) out_str += header_rst('Description', 4) for i, exercise in enumerate(self.exercises): if num_exercises > 1: out_str += header_rst(f'Level {i + 1}', 5) out_str += exercise.exercise.to_rst() return out_str
def _rst(self) -> str: out_str = header_rst(self.title, 3) out_str += self._youtube_rst out_str += self._description_rst out_str += self._resources_rst out_str += self._answers_rst return out_str
def _answers_rst(self) -> str: out_str = '' exc_with_answers = [ exc for exc in self.exercises if exc.answers_content ] if exc_with_answers: num_exercises = len(exc_with_answers) out_str += header_rst('Answers', 4) for i, exercise in enumerate(self.exercises): if not exercise.answers_content: continue if num_exercises > 1: out_str += header_rst(f'Level {i + 1}', 5) out_str += exercise.answers.to_rst() return out_str
def _resources_youtube(self) -> str: out_str = '' if self.resources: out_str += (header_rst('Resources', 4) + '\n' + "\n".join([res.to_youtube() for res in self.resources])) return out_str
def _resources_rst(self) -> str: out_str = '' if self.resources: out_str += (header_rst('Resources', 4) + "\n" + "\n".join([res.to_rst() for res in self.resources]) + "\n") return out_str
def to_rst(self) -> str: out_str = header_rst(self.title, 3) if self.youtube_id: out_str += self._youtube_rst else: out_str += self._youtube_alt_rst if self.notes: out_str += self._notes_rst else: out_str += self._notes_alt_rst out_str += self._resources_rst out_str += self._transcript_rst return out_str
def _static_sections_rst(sections_dict: dict, level: int) -> str: this_section_contents = sections_dict["_description"] + "\n\n" sub_section_contents = "" for section_name, section_content in sections_dict.items(): if section_name == "_description": continue if section_name == "_items": # Content for this section, not another subsection section_content = cast(List[LectureResource], section_content) for res in section_content: this_section_contents += res.to_rst() continue # Content for sub-section section_content = cast(dict, section_content) sub_section_contents += header_rst(section_name, level) sub_section_contents += _static_sections_rst(section_content, level + 1) all_contents = this_section_contents + sub_section_contents return all_contents
def _transcript_rst(self) -> str: if not self.youtube_id: return '' try: transcript = self.transcript except TranscriptsDisabled: return '' out_str = header_rst('Transcript', 4) html = transcript.to_html() rst = f""" .. raw:: html {html} | """ out_str += rst return out_str
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
def generate_downloads_rst( generated_metadata_path: pathlib.Path = GENERATED_CONTENT_METADATA_PATH, static_metadata_path: pathlib.Path = STATIC_CONTENT_METADATA_PATH, out_path: pathlib.Path = OUT_PATH, ): source = header_rst("Downloads", 2) all_downloads_url = str(ZIP_FILE.relative_to(DOCSRC_STATIC_PATH)) all_downloads_last_modified = _get_last_modified_all_content( generated_metadata_path, static_metadata_path ) all_downloads_resource = LectureResource( ZIP_FILE_NAME, static_url=all_downloads_url, updated=all_downloads_last_modified ) source += f""" Find all the individual files for course content on this page. Files will be updated over time so the last updated date is displayed next to the file. To download all the content as a zip file, use the following link: """ source += all_downloads_resource.to_rst() source += _generate_generated_content_rst(generated_metadata_path) source += _generate_static_content_rst(static_metadata_path) out_path.write_text(source)
def _notes_youtube(self) -> str: out_str = header_rst(self.notes_section_name, 4) out_str += self.notes.to_youtube() return out_str