Skip to content

Commit

Permalink
feat: allow customizing outer container style
Browse files Browse the repository at this point in the history
This addresses #30.
  • Loading branch information
gregorias committed Dec 29, 2022
1 parent f155111 commit 4c05902
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,15 @@ for how to fix this.

The plugin accepts the following configuration options:

- `css-files` (default lives in `codehighlighter/main.py`) — the list of CSS styles to use.
- `shortcut` (e.g. `ctrl+;`) — this sets the shortcut that triggers this plugin.
- `default-highlighter` (`highlight.js`, `pygments`, ``) — this sets the default highlighting mechanism.
* `block-style` (default: "display:flex; justify-content:center;") — The CSS
style applied to the outer most container of a block code snippet. The
default centers the block.
* `css-files` (default lives in `codehighlighter/main.py`) — the list of CSS
styles to use.
* `shortcut` (e.g. `ctrl+;`) — this sets the shortcut that triggers this
plugin.
* `default-highlighter` (`highlight.js`, `pygments`, ``) — this sets the
default highlighting mechanism.

### Custom styles

Expand Down
1 change: 1 addition & 0 deletions codehighlighter/config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"block-style": "display:flex; justify-content:center;",
"css-files": [
"_ch-pygments-solarized.css",
"_ch-hljs-solarized.css"
Expand Down
23 changes: 16 additions & 7 deletions codehighlighter/highlighter.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ def send(self, new_nodes: List[bs4.PageElement]):
dfs_stack.send(maybe_more_nodes)


def format_code_hljs(language: str, code: str) -> bs4.Tag:
def format_code_hljs(
language: str,
code: str,
block_style: str = "display:flex; justify-content:center;") -> bs4.Tag:
"""Formats the code snippet.
Returns:
Expand All @@ -75,7 +78,8 @@ def format_code_hljs(language: str, code: str) -> bs4.Tag:
else:
code_tag['class'] = ['language-' + language]
pre_tag = soup.new_tag('pre')
pre_tag['style'] = "display:flex; justify-content:center;"
if block_style:
pre_tag['style'] = block_style
code_tag.append(soup)
pre_tag.append(code_tag)
walk(pre_tag, walk_func)
Expand Down Expand Up @@ -106,8 +110,12 @@ class DISPLAY_STYLE(Enum):
INLINE = 2


def format_code_pygments(language: str, display_style: DISPLAY_STYLE,
code: str) -> bs4.BeautifulSoup:
def format_code_pygments(
language: str,
display_style: DISPLAY_STYLE,
code: str,
block_style: str = "display:flex; justify-content:center;"
) -> bs4.BeautifulSoup:
lexer = pygments.lexers.get_lexer_by_name(language)
if display_style is DISPLAY_STYLE.INLINE:
htmlf = pygments.formatters.get_formatter_by_name('html', nowrap=True)
Expand All @@ -122,9 +130,10 @@ def format_code_pygments(language: str, display_style: DISPLAY_STYLE,
highlighted = highlighted.removeprefix('<pre>')
highlighted = highlighted.removesuffix('</pre>')
highlighted = highlighted.removeprefix('<span></span>')
highlighted = (
f'<div class="pygments" style="display:flex; justify-content:center;">\n <pre><code class="nohighlight">{highlighted}'
+ '</code></pre>\n</div>\n')
style_attr = f' style="{block_style}"' if block_style else ""
highlighted = (f'<div class="pygments"{style_attr}>\n' +
f' <pre><code class="nohighlight">{highlighted}' +
'</code></pre>\n</div>\n')
highlighted = apply_eye_candy(highlighted, language=language)
if display_style is DISPLAY_STYLE.INLINE:
highlighted = remove_spurious_inline_newline(highlighted)
Expand Down
6 changes: 4 additions & 2 deletions codehighlighter/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,13 @@ def show_dialogs() -> Optional[FormatConfig]:
return None

def format(args: FormatConfig, code) -> Union[bs4.Tag, bs4.BeautifulSoup]:
block_style = get_config("block-style",
"display:flex; justify-content:center;")
if isinstance(args, HljsConfig):
return format_code_hljs(args.language, code)
return format_code_hljs(args.language, code, block_style)
else:
return format_code_pygments(args.language, args.display_style,
code)
code, block_style)

transform_selection(editor, note, currentFieldNo, show_dialogs,
format) # type: ignore
Expand Down

0 comments on commit 4c05902

Please sign in to comment.