Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Sep 16, 2024
1 parent fafa016 commit e364a11
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
12 changes: 8 additions & 4 deletions nbconvert/exporters/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@
from nbformat import NotebookNode

from nbconvert.filters.highlight import Highlight2HTML
from nbconvert.filters.markdown_mistune import IPythonRenderer, MarkdownWithMath
from nbconvert.filters.markdown_mistune import (
IPythonRenderer,
MarkdownWithMath,
extract_titles_from_markdown_input,
)
from nbconvert.filters.widgetsdatatypefilter import WidgetsDataTypeFilter
from nbconvert.utils.iso639_1 import iso639_1

from .templateexporter import TemplateExporter
from nbconvert.filters.markdown_mistune import extract_titles_from_markdown_input


def find_lab_theme(theme_name):
"""
Expand Down Expand Up @@ -257,8 +261,8 @@ def from_notebook_node( # type:ignore[explicit-override, override]
"highlight_code", Highlight2HTML(pygments_lexer=lexer, parent=self)
)
markdown_collection = ""
for cell in nb.cells:
if cell.cell_type == 'markdown':
for cell in nb.cells:
if cell.cell_type == "markdown":
markdown_collection = markdown_collection + cell.source + "\n"

resources["tableofcontents"] = extract_titles_from_markdown_input(markdown_collection)
Expand Down
10 changes: 5 additions & 5 deletions nbconvert/exporters/templateexporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,8 +684,8 @@ def _init_resources(self, resources):
return resources

def include_tableofcontents(self, resources):
#if len(resources["tableofcontents"])>0:
return True
#else:
#return False
# if len(resources["tableofcontents"])>0:
return True

# else:
# return False
36 changes: 18 additions & 18 deletions nbconvert/filters/markdown_mistune.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
from typing import Any, Callable, Dict, Iterable, Match, Optional, Tuple

import bs4
import mistune
from mistune.renderers.markdown import MarkdownRenderer
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexer import Lexer
from pygments.lexers import get_lexer_by_name
from pygments.util import ClassNotFound

from nbconvert.filters.strings import add_anchor
import mistune
from mistune.renderers.markdown import MarkdownRenderer

try: # for Mistune >= 3.0
from mistune import ( # type:ignore[attr-defined]
Expand Down Expand Up @@ -490,6 +490,7 @@ def markdown2html_mistune(source: str) -> str:
"""Convert a markdown string to HTML using mistune"""
return MarkdownWithMath(renderer=IPythonRenderer(escape=False)).render(source)


# Custom renderer to capture headings
class HeadingExtractor(MarkdownRenderer):
def __init__(self):
Expand All @@ -498,34 +499,33 @@ def __init__(self):

def heading(self, text, level):
self.headings.append((level, text))
return '' # We return an empty string to avoid outputting the headings
return "" # We return an empty string to avoid outputting the headings



def extract_titles_from_markdown_input (markdown_input):
def extract_titles_from_markdown_input(markdown_input):
# Markdown_input is a single string with all the markdown content concatenated
# Initiate list of titles
titles_array = []

# Instantiate the custom renderer
renderer = HeadingExtractor()

# Create a Markdown parser with the custom renderer
extract_titles = mistune.create_markdown(renderer=renderer)


# Parse the Markdown
extract_titles(markdown_input)

# renderer.headings is an array for each markdown element
#print(renderer.headings)
# print(renderer.headings)

# Extracted headings
for level, title in renderer.headings:
children = title['children']
attrs = title['attrs']
raw_text= children[0]['raw']
level= attrs['level']
children = title["children"]
attrs = title["attrs"]
raw_text = children[0]["raw"]
level = attrs["level"]
titles_array.append([level, raw_text])
print(titles_array)
return titles_array

print(titles_array)
return titles_array

0 comments on commit e364a11

Please sign in to comment.