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 Nov 6, 2024
1 parent 8623ce8 commit 784598d
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 33 deletions.
28 changes: 16 additions & 12 deletions Untitled.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
}
],
"source": [
"import mistune\n",
"import re\n",
"\n",
"import mistune\n",
"\n",
"\n",
"class TitleExtractorRenderer(mistune.HTMLRenderer):\n",
" def __init__(self):\n",
" super().__init__()\n",
Expand All @@ -28,16 +30,17 @@
" # Override the heading method for Markdown headings\n",
" def heading(self, text, level):\n",
" self.titles.append((level, text))\n",
" return '' # return empty since we only want to extract\n",
" return \"\" # return empty since we only want to extract\n",
"\n",
" # Override the html_block method to handle raw HTML\n",
" def html_block(self, html):\n",
" # Regex to find HTML headings <h1> to <h6>\n",
" matches = re.findall(r'<h([1-6])>(.*?)</h\\1>', html, re.IGNORECASE)\n",
" matches = re.findall(r\"<h([1-6])>(.*?)</h\\1>\", html, re.IGNORECASE)\n",
" for level, text in matches:\n",
" self.titles.append((int(level), text))\n",
" print(text);\n",
" return '' # return empty as we're only extracting titles\n",
" print(text)\n",
" return \"\" # return empty as we're only extracting titles\n",
"\n",
"\n",
"# Create an instance of the renderer and Markdown parser\n",
"renderer = TitleExtractorRenderer()\n",
Expand All @@ -61,7 +64,7 @@
"# Print the extracted titles\n",
"print(renderer.titles)\n",
"print(renderer.heading)\n",
"print(renderer.html_block)\n"
"print(renderer.html_block)"
]
},
{
Expand All @@ -72,7 +75,7 @@
"outputs": [],
"source": [
"import mistune\n",
"import re\n",
"\n",
"\n",
"class TitleExtractorRenderer(mistune.HTMLRenderer):\n",
" def __init__(self):\n",
Expand All @@ -82,16 +85,17 @@
" # Override the heading method for Markdown headings\n",
" def heading(self, text, level):\n",
" self.titles.append((level, text))\n",
" return '' # return empty since we only want to extract\n",
" return \"\" # return empty since we only want to extract\n",
"\n",
" # Override the html_block method to handle raw HTML\n",
" def html_block(self, html):\n",
" # Regex to find HTML headings <h1> to <h6>\n",
" matches = re.findall(r'<h([1-6])>(.*?)</h\\1>', html, re.IGNORECASE)\n",
" matches = re.findall(r\"<h([1-6])>(.*?)</h\\1>\", html, re.IGNORECASE)\n",
" for level, text in matches:\n",
" self.titles.append((int(level), text))\n",
" print(text);\n",
" return '' # return empty as we're only extracting titles\n",
" print(text)\n",
" return \"\" # return empty as we're only extracting titles\n",
"\n",
"\n",
"# Create an instance of the renderer and Markdown parser\n",
"renderer = TitleExtractorRenderer()\n",
Expand All @@ -115,7 +119,7 @@
"# Print the extracted titles\n",
"print(renderer.titles)\n",
"print(renderer.heading)\n",
"print(renderer.html_block)\n"
"print(renderer.html_block)"
]
}
],
Expand Down
28 changes: 16 additions & 12 deletions Untitled2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
}
],
"source": [
"import mistune\n",
"import re\n",
"\n",
"import mistune\n",
"from mistune.renderers.markdown import MarkdownRenderer\n",
"from nbformat import NotebookNode\n",
"\n",
"\n",
"class HeadingExtractor(MarkdownRenderer):\n",
" \"\"\"A renderer to capture headings\"\"\"\n",
"\n",
Expand All @@ -34,12 +36,14 @@
"\n",
" def heading(self, text, level):\n",
" \"\"\"Return an empty string for the headings to avoid outputting them.\"\"\"\n",
" matches = re.findall(r'<h[1-6]>.*?<\\/h[1-6]>', text)\n",
" matches = re.findall(r\"<h[1-6]>.*?<\\/h[1-6]>\", text)\n",
" print(matches)\n",
" \n",
"\n",
" for level, text in matches:\n",
" # You can use int() to convert the level to an integer\n",
" self.headings.append((int(level), text.strip())) # .strip() removes any leading/trailing whitespace\n",
" # You can use int() to convert the level to an integer\n",
" self.headings.append(\n",
" (int(level), text.strip())\n",
" ) # .strip() removes any leading/trailing whitespace\n",
" print(f\"Level: {level}, Text: {text.strip()}\")\n",
" # self.headings.append((level, text))\n",
" return \"\"\n",
Expand All @@ -56,15 +60,15 @@
" if cell.cell_type == \"markdown\":\n",
" lines = cell.source.splitlines()\n",
" for line in lines:\n",
" newline= line\n",
" \n",
" if line.startswith('#'):\n",
" newline = line\n",
"\n",
" if line.startswith(\"#\"):\n",
" newline = mistune.html(newline)\n",
" \n",
" #print(\"line:\", line)\n",
" #print('newline:', newline)\n",
"\n",
" # print(\"line:\", line)\n",
" # print('newline:', newline)\n",
" markdown_collection = markdown_collection + newline.strip() + \"\\n\"\n",
" #print(markdown_collection)\n",
" # print(markdown_collection)\n",
" titles_array = []\n",
" renderer = HeadingExtractor()\n",
" extract_titles = mistune.create_markdown(renderer=renderer)\n",
Expand Down
2 changes: 1 addition & 1 deletion nbconvert/exporters/slides.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def preprocess(self, nb, resources=None):

in_fragment = False

for index, cell in enumerate(nb.cells[first_slide_ix + 1:], start=(first_slide_ix + 1)):
for index, cell in enumerate(nb.cells[first_slide_ix + 1 :], start=(first_slide_ix + 1)):
previous_cell = nb.cells[index - 1]

# Slides are <section> elements in the HTML, subslides (the vertically
Expand Down
2 changes: 1 addition & 1 deletion nbconvert/filters/ansi.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def _ansi2anything(text, converter):
pass # Invalid color specification
else:
pass # Not a color code
chunk, text = text[: m.start()], text[m.end():]
chunk, text = text[: m.start()], text[m.end() :]
else:
chunk, text = text, ""

Expand Down
2 changes: 1 addition & 1 deletion nbconvert/filters/citation.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def citation2latex(s):
outtext = ""
startpos = 0
for citation in parser.citelist:
outtext += s[startpos: citation[1]]
outtext += s[startpos : citation[1]]
outtext += "\\cite{%s}" % citation[0]
startpos = citation[2] if len(citation) == 3 else -1
outtext += s[startpos:] if startpos != -1 else ""
Expand Down
4 changes: 2 additions & 2 deletions nbconvert/filters/markdown_mistune.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

import bs4
import mistune
from nbformat import NotebookNode
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
from nbformat import NotebookNode

try: # for Mistune >= 3.0
from mistune import ( # type:ignore[attr-defined]
Expand Down Expand Up @@ -385,7 +385,7 @@ def _embed_image_or_attachment(self, src: str) -> str:

attachment_prefix = "attachment:"
if src.startswith(attachment_prefix):
name = src[len(attachment_prefix):]
name = src[len(attachment_prefix) :]

if name not in self.attachments:
msg = f"missing attachment: {name}"
Expand Down
8 changes: 4 additions & 4 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,15 @@ def assert_big_text_equal(a, b, chunk_size=80):
to give better info than vanilla assertEqual for large text blobs.
"""
for i in range(0, len(a), chunk_size):
chunk_a = a[i: i + chunk_size]
chunk_b = b[i: i + chunk_size]
chunk_a = a[i : i + chunk_size]
chunk_b = b[i : i + chunk_size]
assert chunk_a == chunk_b, "[offset: %i]\n%r != \n%r" % (i, chunk_a, chunk_b)

if len(a) > len(b):
raise AssertionError(
"Length doesn't match (%i > %i). Extra text:\n%r" % (len(a), len(b), a[len(b):])
"Length doesn't match (%i > %i). Extra text:\n%r" % (len(a), len(b), a[len(b) :])
)
if len(a) < len(b):
raise AssertionError(
"Length doesn't match (%i < %i). Extra text:\n%r" % (len(a), len(b), a[len(b):])
"Length doesn't match (%i < %i). Extra text:\n%r" % (len(a), len(b), a[len(b) :])
)

0 comments on commit 784598d

Please sign in to comment.