Skip to content

Commit

Permalink
Reformat all Python files with ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
mitya57 committed Dec 8, 2024
1 parent 5f4f11f commit dd6275c
Show file tree
Hide file tree
Showing 13 changed files with 358 additions and 330 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,7 @@ jobs:
- uses: actions/setup-python@v5
- name: Install ruff
run: python -m pip install ruff
- name: Run ruff
- name: Run ruff check
run: ruff check --select F,E,W,I,UP --target-version py310 .
- name: Run ruff format
- run: ruff format --diff .
20 changes: 10 additions & 10 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,45 @@
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
sys.path.insert(0, os.path.abspath('..'))
sys.path.insert(0, os.path.abspath(".."))

# -- General configuration ------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
"sphinx.ext.autodoc",
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
templates_path = ["_templates"]

# The suffix of source filenames.
source_suffix = '.rst'
source_suffix = ".rst"

# The master toctree document.
master_doc = 'index'
master_doc = "index"

# General information about the project.
project = 'Python-Markups'
copyright = '2023, Dmitry Shachnev'
project = "Python-Markups"
copyright = "2023, Dmitry Shachnev"

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
from markups import __version__, __version_tuple__ # noqa: E402

# The short X.Y version.
version = '{}.{}'.format(*__version_tuple__)
version = "{}.{}".format(*__version_tuple__)
# The full version, including alpha/beta/rc tags.
release = __version__

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'
pygments_style = "sphinx"

# -- Options for HTML output ----------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
html_theme = 'nature'
html_theme = "nature"
35 changes: 21 additions & 14 deletions markup2html.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,33 @@ def export_file(args: argparse.Namespace) -> None:
with open(args.input_file) as input:
text = input.read()
if not markup:
sys.exit('Markup not available.')
sys.exit("Markup not available.")
converted = markup.convert(text)

html = converted.get_whole_html(include_stylesheet=args.include_stylesheet,
fallback_title=args.fallback_title,
webenv=args.web_environment)
html = converted.get_whole_html(
include_stylesheet=args.include_stylesheet,
fallback_title=args.fallback_title,
webenv=args.web_environment,
)

with open(args.output_file, 'w') as output:
with open(args.output_file, "w") as output:
output.write(html)


if __name__ == '__main__':
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--web-environment', help='export for web environment',
action='store_true')
parser.add_argument('--include-stylesheet', help='embed the stylesheet into html',
action='store_true')
parser.add_argument('--fallback-title', help='fallback title of the HTML document',
metavar='TITLE')
parser.add_argument('input_file', help='input file')
parser.add_argument('output_file', help='output file')
parser.add_argument(
"--web-environment", help="export for web environment", action="store_true"
)
parser.add_argument(
"--include-stylesheet",
help="embed the stylesheet into html",
action="store_true",
)
parser.add_argument(
"--fallback-title", help="fallback title of the HTML document", metavar="TITLE"
)
parser.add_argument("input_file", help="input file")
parser.add_argument("output_file", help="output file")
args = parser.parse_args()
export_file(args)
8 changes: 3 additions & 5 deletions markups/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from markups.textile import TextileMarkup

__version_tuple__ = (4, 0, 0)
__version__ = '.'.join(map(str, __version_tuple__))
__version__ = ".".join(map(str, __version_tuple__))

__all__ = [
"AbstractMarkup",
Expand Down Expand Up @@ -63,15 +63,13 @@ def get_available_markups() -> list[type[AbstractMarkup]]:
@overload
def get_markup_for_file_name(
filename: str, return_class: Literal[False] = False
) -> AbstractMarkup | None:
...
) -> AbstractMarkup | None: ...


@overload
def get_markup_for_file_name(
filename: str, return_class: Literal[True]
) -> type[AbstractMarkup] | None:
...
) -> type[AbstractMarkup] | None: ...


def get_markup_for_file_name(
Expand Down
21 changes: 15 additions & 6 deletions markups/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ class ConvertedMarkup:
method, usually it should not be instantiated directly.
"""

def __init__(self, body: str, title: str = '',
stylesheet: str = '', javascript: str = ''):
def __init__(
self, body: str, title: str = "", stylesheet: str = "", javascript: str = ""
):
self.title = title
self.stylesheet = stylesheet
self.javascript = javascript
Expand Down Expand Up @@ -99,8 +100,13 @@ def get_javascript(self, webenv: bool = False) -> str:
"""
return self.javascript

def get_whole_html(self, custom_headers: str = '', include_stylesheet: bool = True,
fallback_title: str = '', webenv: bool = False) -> str:
def get_whole_html(
self,
custom_headers: str = "",
include_stylesheet: bool = True,
fallback_title: str = "",
webenv: bool = False,
) -> str:
"""
:returns: the full contents of the HTML document (unless overridden
this is a combination of the previous methods)
Expand All @@ -114,8 +120,11 @@ def get_whole_html(self, custom_headers: str = '', include_stylesheet: bool = Tr
:param webenv: like in :meth:`~.ConvertedMarkup.get_javascript`
above
"""
stylesheet = ('<style type="text/css">\n' + self.get_stylesheet()
+ '</style>\n' if include_stylesheet else '')
stylesheet = (
'<style type="text/css">\n' + self.get_stylesheet() + "</style>\n"
if include_stylesheet
else ""
)

context = {
"body": self.get_document_body(),
Expand Down
27 changes: 14 additions & 13 deletions markups/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,32 @@

# Some common constants and functions
(LANGUAGE_HOME_PAGE, MODULE_HOME_PAGE, SYNTAX_DOCUMENTATION) = range(3)
CONFIGURATION_DIR = (os.getenv('XDG_CONFIG_HOME') or os.getenv('APPDATA') or
os.path.expanduser('~/.config'))
CONFIGURATION_DIR = (
os.getenv("XDG_CONFIG_HOME")
or os.getenv("APPDATA")
or os.path.expanduser("~/.config")
)
MATHJAX2_LOCAL_URLS = (
'file:///usr/share/javascript/mathjax/MathJax.js', # Debian libjs-mathjax
'file:///usr/share/mathjax2/MathJax.js', # Arch Linux mathjax2
"file:///usr/share/javascript/mathjax/MathJax.js", # Debian libjs-mathjax
"file:///usr/share/mathjax2/MathJax.js", # Arch Linux mathjax2
)
MATHJAX3_LOCAL_URLS = (
'file:///usr/share/mathjax/tex-chtml.js', # Arch Linux mathjax
"file:///usr/share/mathjax/tex-chtml.js", # Arch Linux mathjax
)
MATHJAX_WEB_URL = 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js'
MATHJAX_WEB_URL = "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"

PYGMENTS_STYLE = 'default'
PYGMENTS_STYLE = "default"


def get_pygments_stylesheet(
selector: str | None, style: str | None = None
) -> str:
def get_pygments_stylesheet(selector: str | None, style: str | None = None) -> str:
if style is None:
style = PYGMENTS_STYLE
if style == '':
return ''
if style == "":
return ""
try:
from pygments.formatters import HtmlFormatter
except ImportError:
return ''
return ""
else:
defs = HtmlFormatter(style=style).get_style_defs(selector)
assert isinstance(defs, str)
Expand Down
Loading

0 comments on commit dd6275c

Please sign in to comment.