Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command to auto-generate CSS files from pygments styles in settings #641

Closed
2 changes: 2 additions & 0 deletions codethesaurus/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,5 @@

# Configure Django App for Heroku.
django_on_heroku.settings(locals(), test_runner=False, databases=False, staticfiles=True, logging=True)

PYGMENTS_STYLES = ['colorful', 'default', 'sas', 'xcode', 'rrt']
39 changes: 39 additions & 0 deletions web/management/commands/generate_css_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os
from django.core.management.base import BaseCommand
from django.conf import settings

from pygments.formatters.html import HtmlFormatter

class Command(BaseCommand):
help = 'Generate static CSS files from pygments styles'

def handle(self, *args, **options):
for style in settings.PYGMENTS_STYLES:
self.generate_file(style)

def generate_file(self, style):
css = HtmlFormatter.get_style_defs(HtmlFormatter(style=style))

# we need to prepend the `syntax` class before every class selector in the stylesheet to make sure that the styles get applied
# only to our code snippet and not any other DOM element that happens to have the same class name.
modified_css = []
for line in css.split("\n"):
if line[0] == '.':
line = '.syntax ' + line
modified_css.append(line)
Shreya-7 marked this conversation as resolved.
Show resolved Hide resolved

css_file_path = os.path.join(
'web',
'static',
'css',
f'pygments_{style}.css'
)

# remove existing stylesheet so that it can be replaced with newer style versions or fixes
if os.path.exists(css_file_path):
self.stdout.write(f'Removing stylesheet: {css_file_path} for style: {style}')
os.remove(css_file_path)

Shreya-7 marked this conversation as resolved.
Show resolved Hide resolved
with open(css_file_path, 'w') as file:
file.write('\n'.join(modified_css)+'\n')
self.stdout.write(f'Created css stylesheet: {css_file_path}')
1 change: 0 additions & 1 deletion web/static/css/pygments_colorful.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ span.linenos { color: inherit; background-color: transparent; padding-left: 5px;
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.syntax .hll { background-color: #ffffcc }
.syntax { background: #ffffff; }
.syntax .c { color: #888888 } /* Comment */
.syntax .err { color: #FF0000; background-color: #FFAAAA } /* Error */
.syntax .k { color: #008800; font-weight: bold } /* Keyword */
Expand Down
73 changes: 73 additions & 0 deletions web/static/css/pygments_default.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.syntax .hll { background-color: #ffffcc }
.syntax .c { color: #408080; font-style: italic } /* Comment */
.syntax .err { border: 1px solid #FF0000 } /* Error */
.syntax .k { color: #008000; font-weight: bold } /* Keyword */
.syntax .o { color: #666666 } /* Operator */
.syntax .ch { color: #408080; font-style: italic } /* Comment.Hashbang */
.syntax .cm { color: #408080; font-style: italic } /* Comment.Multiline */
.syntax .cp { color: #BC7A00 } /* Comment.Preproc */
.syntax .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */
.syntax .c1 { color: #408080; font-style: italic } /* Comment.Single */
.syntax .cs { color: #408080; font-style: italic } /* Comment.Special */
.syntax .gd { color: #A00000 } /* Generic.Deleted */
.syntax .ge { font-style: italic } /* Generic.Emph */
.syntax .gr { color: #FF0000 } /* Generic.Error */
.syntax .gh { color: #000080; font-weight: bold } /* Generic.Heading */
.syntax .gi { color: #00A000 } /* Generic.Inserted */
.syntax .go { color: #888888 } /* Generic.Output */
.syntax .gp { color: #000080; font-weight: bold } /* Generic.Prompt */
.syntax .gs { font-weight: bold } /* Generic.Strong */
.syntax .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
.syntax .gt { color: #0044DD } /* Generic.Traceback */
.syntax .kc { color: #008000; font-weight: bold } /* Keyword.Constant */
.syntax .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */
.syntax .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */
.syntax .kp { color: #008000 } /* Keyword.Pseudo */
.syntax .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */
.syntax .kt { color: #B00040 } /* Keyword.Type */
.syntax .m { color: #666666 } /* Literal.Number */
.syntax .s { color: #BA2121 } /* Literal.String */
.syntax .na { color: #7D9029 } /* Name.Attribute */
.syntax .nb { color: #008000 } /* Name.Builtin */
.syntax .nc { color: #0000FF; font-weight: bold } /* Name.Class */
.syntax .no { color: #880000 } /* Name.Constant */
.syntax .nd { color: #AA22FF } /* Name.Decorator */
.syntax .ni { color: #999999; font-weight: bold } /* Name.Entity */
.syntax .ne { color: #D2413A; font-weight: bold } /* Name.Exception */
.syntax .nf { color: #0000FF } /* Name.Function */
.syntax .nl { color: #A0A000 } /* Name.Label */
.syntax .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
.syntax .nt { color: #008000; font-weight: bold } /* Name.Tag */
.syntax .nv { color: #19177C } /* Name.Variable */
.syntax .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
.syntax .w { color: #bbbbbb } /* Text.Whitespace */
.syntax .mb { color: #666666 } /* Literal.Number.Bin */
.syntax .mf { color: #666666 } /* Literal.Number.Float */
.syntax .mh { color: #666666 } /* Literal.Number.Hex */
.syntax .mi { color: #666666 } /* Literal.Number.Integer */
.syntax .mo { color: #666666 } /* Literal.Number.Oct */
.syntax .sa { color: #BA2121 } /* Literal.String.Affix */
.syntax .sb { color: #BA2121 } /* Literal.String.Backtick */
.syntax .sc { color: #BA2121 } /* Literal.String.Char */
.syntax .dl { color: #BA2121 } /* Literal.String.Delimiter */
.syntax .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */
.syntax .s2 { color: #BA2121 } /* Literal.String.Double */
.syntax .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
.syntax .sh { color: #BA2121 } /* Literal.String.Heredoc */
.syntax .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
.syntax .sx { color: #008000 } /* Literal.String.Other */
.syntax .sr { color: #BB6688 } /* Literal.String.Regex */
.syntax .s1 { color: #BA2121 } /* Literal.String.Single */
.syntax .ss { color: #19177C } /* Literal.String.Symbol */
.syntax .bp { color: #008000 } /* Name.Builtin.Pseudo */
.syntax .fm { color: #0000FF } /* Name.Function.Magic */
.syntax .vc { color: #19177C } /* Name.Variable.Class */
.syntax .vg { color: #19177C } /* Name.Variable.Global */
.syntax .vi { color: #19177C } /* Name.Variable.Instance */
.syntax .vm { color: #19177C } /* Name.Variable.Magic */
.syntax .il { color: #666666 } /* Literal.Number.Integer.Long */
42 changes: 42 additions & 0 deletions web/static/css/pygments_rrt.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.syntax .hll { background-color: #0000ff }
.syntax .c { color: #00ff00 } /* Comment */
.syntax .k { color: #ff0000 } /* Keyword */
.syntax .ch { color: #00ff00 } /* Comment.Hashbang */
.syntax .cm { color: #00ff00 } /* Comment.Multiline */
.syntax .cp { color: #e5e5e5 } /* Comment.Preproc */
.syntax .cpf { color: #00ff00 } /* Comment.PreprocFile */
.syntax .c1 { color: #00ff00 } /* Comment.Single */
.syntax .cs { color: #00ff00 } /* Comment.Special */
.syntax .kc { color: #ff0000 } /* Keyword.Constant */
.syntax .kd { color: #ff0000 } /* Keyword.Declaration */
.syntax .kn { color: #ff0000 } /* Keyword.Namespace */
.syntax .kp { color: #ff0000 } /* Keyword.Pseudo */
.syntax .kr { color: #ff0000 } /* Keyword.Reserved */
.syntax .kt { color: #ee82ee } /* Keyword.Type */
.syntax .s { color: #87ceeb } /* Literal.String */
.syntax .no { color: #7fffd4 } /* Name.Constant */
.syntax .nf { color: #ffff00 } /* Name.Function */
.syntax .nv { color: #eedd82 } /* Name.Variable */
.syntax .sa { color: #87ceeb } /* Literal.String.Affix */
.syntax .sb { color: #87ceeb } /* Literal.String.Backtick */
.syntax .sc { color: #87ceeb } /* Literal.String.Char */
.syntax .dl { color: #87ceeb } /* Literal.String.Delimiter */
.syntax .sd { color: #87ceeb } /* Literal.String.Doc */
.syntax .s2 { color: #87ceeb } /* Literal.String.Double */
.syntax .se { color: #87ceeb } /* Literal.String.Escape */
.syntax .sh { color: #87ceeb } /* Literal.String.Heredoc */
.syntax .si { color: #87ceeb } /* Literal.String.Interpol */
.syntax .sx { color: #87ceeb } /* Literal.String.Other */
.syntax .sr { color: #87ceeb } /* Literal.String.Regex */
.syntax .s1 { color: #87ceeb } /* Literal.String.Single */
.syntax .ss { color: #87ceeb } /* Literal.String.Symbol */
.syntax .fm { color: #ffff00 } /* Name.Function.Magic */
.syntax .vc { color: #eedd82 } /* Name.Variable.Class */
.syntax .vg { color: #eedd82 } /* Name.Variable.Global */
.syntax .vi { color: #eedd82 } /* Name.Variable.Instance */
.syntax .vm { color: #eedd82 } /* Name.Variable.Magic */
64 changes: 64 additions & 0 deletions web/static/css/pygments_sas.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.syntax .hll { background-color: #ffffcc }
.syntax .c { color: #008800; font-style: italic } /* Comment */
.syntax .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.syntax .g { color: #2c2cff } /* Generic */
.syntax .k { color: #2c2cff } /* Keyword */
.syntax .x { background-color: #ffffe0 } /* Other */
.syntax .ch { color: #008800; font-style: italic } /* Comment.Hashbang */
.syntax .cm { color: #008800; font-style: italic } /* Comment.Multiline */
.syntax .cp { color: #008800; font-style: italic } /* Comment.Preproc */
.syntax .cpf { color: #008800; font-style: italic } /* Comment.PreprocFile */
.syntax .c1 { color: #008800; font-style: italic } /* Comment.Single */
.syntax .cs { color: #008800; font-style: italic } /* Comment.Special */
.syntax .gd { color: #2c2cff } /* Generic.Deleted */
.syntax .ge { color: #008800 } /* Generic.Emph */
.syntax .gr { color: #d30202 } /* Generic.Error */
.syntax .gh { color: #2c2cff } /* Generic.Heading */
.syntax .gi { color: #2c2cff } /* Generic.Inserted */
.syntax .go { color: #2c2cff } /* Generic.Output */
.syntax .gp { color: #2c2cff } /* Generic.Prompt */
.syntax .gs { color: #2c2cff } /* Generic.Strong */
.syntax .gu { color: #2c2cff } /* Generic.Subheading */
.syntax .gt { color: #2c2cff } /* Generic.Traceback */
.syntax .kc { color: #2c2cff; font-weight: bold } /* Keyword.Constant */
.syntax .kd { color: #2c2cff } /* Keyword.Declaration */
.syntax .kn { color: #2c2cff } /* Keyword.Namespace */
.syntax .kp { color: #2c2cff } /* Keyword.Pseudo */
.syntax .kr { color: #353580; font-weight: bold } /* Keyword.Reserved */
.syntax .kt { color: #2c2cff } /* Keyword.Type */
.syntax .m { color: #2e8b57; font-weight: bold } /* Literal.Number */
.syntax .s { color: #800080 } /* Literal.String */
.syntax .nb { color: #2c2cff } /* Name.Builtin */
.syntax .nf { font-weight: bold; font-style: italic } /* Name.Function */
.syntax .nv { color: #2c2cff; font-weight: bold } /* Name.Variable */
.syntax .w { color: #bbbbbb } /* Text.Whitespace */
.syntax .mb { color: #2e8b57; font-weight: bold } /* Literal.Number.Bin */
.syntax .mf { color: #2e8b57; font-weight: bold } /* Literal.Number.Float */
.syntax .mh { color: #2e8b57; font-weight: bold } /* Literal.Number.Hex */
.syntax .mi { color: #2e8b57; font-weight: bold } /* Literal.Number.Integer */
.syntax .mo { color: #2e8b57; font-weight: bold } /* Literal.Number.Oct */
.syntax .sa { color: #800080 } /* Literal.String.Affix */
.syntax .sb { color: #800080 } /* Literal.String.Backtick */
.syntax .sc { color: #800080 } /* Literal.String.Char */
.syntax .dl { color: #800080 } /* Literal.String.Delimiter */
.syntax .sd { color: #800080 } /* Literal.String.Doc */
.syntax .s2 { color: #800080 } /* Literal.String.Double */
.syntax .se { color: #800080 } /* Literal.String.Escape */
.syntax .sh { color: #800080 } /* Literal.String.Heredoc */
.syntax .si { color: #800080 } /* Literal.String.Interpol */
.syntax .sx { color: #800080 } /* Literal.String.Other */
.syntax .sr { color: #800080 } /* Literal.String.Regex */
.syntax .s1 { color: #800080 } /* Literal.String.Single */
.syntax .ss { color: #800080 } /* Literal.String.Symbol */
.syntax .bp { color: #2c2cff } /* Name.Builtin.Pseudo */
.syntax .fm { font-weight: bold; font-style: italic } /* Name.Function.Magic */
.syntax .vc { color: #2c2cff; font-weight: bold } /* Name.Variable.Class */
.syntax .vg { color: #2c2cff; font-weight: bold } /* Name.Variable.Global */
.syntax .vi { color: #2c2cff; font-weight: bold } /* Name.Variable.Instance */
.syntax .vm { color: #2c2cff; font-weight: bold } /* Name.Variable.Magic */
.syntax .il { color: #2e8b57; font-weight: bold } /* Literal.Number.Integer.Long */
67 changes: 67 additions & 0 deletions web/static/css/pygments_xcode.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.syntax .hll { background-color: #ffffcc }
.syntax .c { color: #177500 } /* Comment */
.syntax .err { color: #000000 } /* Error */
.syntax .k { color: #A90D91 } /* Keyword */
.syntax .l { color: #1C01CE } /* Literal */
.syntax .n { color: #000000 } /* Name */
.syntax .o { color: #000000 } /* Operator */
.syntax .ch { color: #177500 } /* Comment.Hashbang */
.syntax .cm { color: #177500 } /* Comment.Multiline */
.syntax .cp { color: #633820 } /* Comment.Preproc */
.syntax .cpf { color: #177500 } /* Comment.PreprocFile */
.syntax .c1 { color: #177500 } /* Comment.Single */
.syntax .cs { color: #177500 } /* Comment.Special */
.syntax .kc { color: #A90D91 } /* Keyword.Constant */
.syntax .kd { color: #A90D91 } /* Keyword.Declaration */
.syntax .kn { color: #A90D91 } /* Keyword.Namespace */
.syntax .kp { color: #A90D91 } /* Keyword.Pseudo */
.syntax .kr { color: #A90D91 } /* Keyword.Reserved */
.syntax .kt { color: #A90D91 } /* Keyword.Type */
.syntax .ld { color: #1C01CE } /* Literal.Date */
.syntax .m { color: #1C01CE } /* Literal.Number */
.syntax .s { color: #C41A16 } /* Literal.String */
.syntax .na { color: #836C28 } /* Name.Attribute */
.syntax .nb { color: #A90D91 } /* Name.Builtin */
.syntax .nc { color: #3F6E75 } /* Name.Class */
.syntax .no { color: #000000 } /* Name.Constant */
.syntax .nd { color: #000000 } /* Name.Decorator */
.syntax .ni { color: #000000 } /* Name.Entity */
.syntax .ne { color: #000000 } /* Name.Exception */
.syntax .nf { color: #000000 } /* Name.Function */
.syntax .nl { color: #000000 } /* Name.Label */
.syntax .nn { color: #000000 } /* Name.Namespace */
.syntax .nx { color: #000000 } /* Name.Other */
.syntax .py { color: #000000 } /* Name.Property */
.syntax .nt { color: #000000 } /* Name.Tag */
.syntax .nv { color: #000000 } /* Name.Variable */
.syntax .ow { color: #000000 } /* Operator.Word */
.syntax .mb { color: #1C01CE } /* Literal.Number.Bin */
.syntax .mf { color: #1C01CE } /* Literal.Number.Float */
.syntax .mh { color: #1C01CE } /* Literal.Number.Hex */
.syntax .mi { color: #1C01CE } /* Literal.Number.Integer */
.syntax .mo { color: #1C01CE } /* Literal.Number.Oct */
.syntax .sa { color: #C41A16 } /* Literal.String.Affix */
.syntax .sb { color: #C41A16 } /* Literal.String.Backtick */
.syntax .sc { color: #2300CE } /* Literal.String.Char */
.syntax .dl { color: #C41A16 } /* Literal.String.Delimiter */
.syntax .sd { color: #C41A16 } /* Literal.String.Doc */
.syntax .s2 { color: #C41A16 } /* Literal.String.Double */
.syntax .se { color: #C41A16 } /* Literal.String.Escape */
.syntax .sh { color: #C41A16 } /* Literal.String.Heredoc */
.syntax .si { color: #C41A16 } /* Literal.String.Interpol */
.syntax .sx { color: #C41A16 } /* Literal.String.Other */
.syntax .sr { color: #C41A16 } /* Literal.String.Regex */
.syntax .s1 { color: #C41A16 } /* Literal.String.Single */
.syntax .ss { color: #C41A16 } /* Literal.String.Symbol */
.syntax .bp { color: #5B269A } /* Name.Builtin.Pseudo */
.syntax .fm { color: #000000 } /* Name.Function.Magic */
.syntax .vc { color: #000000 } /* Name.Variable.Class */
.syntax .vg { color: #000000 } /* Name.Variable.Global */
.syntax .vi { color: #000000 } /* Name.Variable.Instance */
.syntax .vm { color: #000000 } /* Name.Variable.Magic */
.syntax .il { color: #1C01CE } /* Literal.Number.Integer.Long */
2 changes: 1 addition & 1 deletion web/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

<!-- Styles -->
<link rel="stylesheet" href="/static/css/app.css" />
<link rel="stylesheet" href="/static/css/pygments_colorful.css" />
<link rel="stylesheet" href="/static/css/pygments_default.css" />
</head>
<body>
<header>
Expand Down