|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# ----------------------------------------------------------------------------- |
| 3 | +# Copyright (c) 2009- Spyder Kernels Contributors |
| 4 | +# |
| 5 | +# Licensed under the terms of the MIT License |
| 6 | +# (see spyder_kernels/__init__.py for details) |
| 7 | +# ----------------------------------------------------------------------------- |
| 8 | + |
| 9 | +""" |
| 10 | +Style for IPython Console |
| 11 | +""" |
| 12 | + |
| 13 | +# Third party imports |
| 14 | +from pygments.style import Style |
| 15 | +from pygments.token import ( |
| 16 | + Name, |
| 17 | + Keyword, |
| 18 | + Comment, |
| 19 | + String, |
| 20 | + Number, |
| 21 | + Punctuation, |
| 22 | + Operator, |
| 23 | +) |
| 24 | + |
| 25 | + |
| 26 | +def create_pygments_dict(color_scheme_dict): |
| 27 | + """ |
| 28 | + Create a dictionary that saves the given color scheme as a |
| 29 | + Pygments style. |
| 30 | + """ |
| 31 | + |
| 32 | + def give_font_weight(is_bold): |
| 33 | + if is_bold: |
| 34 | + return "bold" |
| 35 | + else: |
| 36 | + return "" |
| 37 | + |
| 38 | + def give_font_style(is_italic): |
| 39 | + if is_italic: |
| 40 | + return "italic" |
| 41 | + else: |
| 42 | + return "" |
| 43 | + |
| 44 | + color_scheme = color_scheme_dict |
| 45 | + |
| 46 | + fon_c, fon_fw, fon_fs = color_scheme["normal"] |
| 47 | + font_color = fon_c |
| 48 | + font_font_weight = give_font_weight(fon_fw) |
| 49 | + font_font_style = give_font_style(fon_fs) |
| 50 | + |
| 51 | + key_c, key_fw, key_fs = color_scheme["keyword"] |
| 52 | + keyword_color = key_c |
| 53 | + keyword_font_weight = give_font_weight(key_fw) |
| 54 | + keyword_font_style = give_font_style(key_fs) |
| 55 | + |
| 56 | + bui_c, bui_fw, bui_fs = color_scheme["builtin"] |
| 57 | + builtin_color = bui_c |
| 58 | + builtin_font_weight = give_font_weight(bui_fw) |
| 59 | + builtin_font_style = give_font_style(bui_fs) |
| 60 | + |
| 61 | + str_c, str_fw, str_fs = color_scheme["string"] |
| 62 | + string_color = str_c |
| 63 | + string_font_weight = give_font_weight(str_fw) |
| 64 | + string_font_style = give_font_style(str_fs) |
| 65 | + |
| 66 | + num_c, num_fw, num_fs = color_scheme["number"] |
| 67 | + number_color = num_c |
| 68 | + number_font_weight = give_font_weight(num_fw) |
| 69 | + number_font_style = give_font_style(num_fs) |
| 70 | + |
| 71 | + com_c, com_fw, com_fs = color_scheme["comment"] |
| 72 | + comment_color = com_c |
| 73 | + comment_font_weight = give_font_weight(com_fw) |
| 74 | + comment_font_style = give_font_style(com_fs) |
| 75 | + |
| 76 | + def_c, def_fw, def_fs = color_scheme["definition"] |
| 77 | + definition_color = def_c |
| 78 | + definition_font_weight = give_font_weight(def_fw) |
| 79 | + definition_font_style = give_font_style(def_fs) |
| 80 | + |
| 81 | + ins_c, ins_fw, ins_fs = color_scheme["instance"] |
| 82 | + instance_color = ins_c |
| 83 | + instance_font_weight = give_font_weight(ins_fw) |
| 84 | + instance_font_style = give_font_style(ins_fs) |
| 85 | + |
| 86 | + font_token = font_font_style + " " + font_font_weight + " " + font_color |
| 87 | + definition_token = ( |
| 88 | + definition_font_style |
| 89 | + + " " |
| 90 | + + definition_font_weight |
| 91 | + + " " |
| 92 | + + definition_color |
| 93 | + ) |
| 94 | + builtin_token = ( |
| 95 | + builtin_font_style + " " + builtin_font_weight + " " + builtin_color |
| 96 | + ) |
| 97 | + instance_token = ( |
| 98 | + instance_font_style + " " + instance_font_weight + " " + instance_color |
| 99 | + ) |
| 100 | + keyword_token = ( |
| 101 | + keyword_font_style + " " + keyword_font_weight + " " + keyword_color |
| 102 | + ) |
| 103 | + comment_token = ( |
| 104 | + comment_font_style + " " + comment_font_weight + " " + comment_color |
| 105 | + ) |
| 106 | + string_token = ( |
| 107 | + string_font_style + " " + string_font_weight + " " + string_color |
| 108 | + ) |
| 109 | + number_token = ( |
| 110 | + number_font_style + " " + number_font_weight + " " + number_color |
| 111 | + ) |
| 112 | + |
| 113 | + syntax_style_dic = { |
| 114 | + Name: font_token.strip(), |
| 115 | + Name.Class: definition_token.strip(), |
| 116 | + Name.Function: definition_token.strip(), |
| 117 | + Name.Builtin: builtin_token.strip(), |
| 118 | + Name.Builtin.Pseudo: instance_token.strip(), |
| 119 | + Keyword: keyword_token.strip(), |
| 120 | + Keyword.Type: builtin_token.strip(), |
| 121 | + Comment: comment_token.strip(), |
| 122 | + String: string_token.strip(), |
| 123 | + Number: number_token.strip(), |
| 124 | + Punctuation: font_token.strip(), |
| 125 | + Operator.Word: keyword_token.strip(), |
| 126 | + } |
| 127 | + |
| 128 | + return syntax_style_dic |
| 129 | + |
| 130 | + |
| 131 | +def create_style_class(color_scheme_dict): |
| 132 | + """Create a Pygments Style class with the given color scheme.""" |
| 133 | + |
| 134 | + class StyleClass(Style): |
| 135 | + default_style = "" |
| 136 | + styles = create_pygments_dict(color_scheme_dict) |
| 137 | + |
| 138 | + return StyleClass |
0 commit comments