diff --git a/sphinx-prompt/__init__.py b/sphinx-prompt/__init__.py index 7dfaad0..0908dda 100644 --- a/sphinx-prompt/__init__.py +++ b/sphinx-prompt/__init__.py @@ -13,6 +13,35 @@ from pygments.formatters import HtmlFormatter +class PromptCache: + + def __init__(self): + self.clear() + + def clear(self, *ignored): + # ignored parameters allow this method to be used as event handler + self.next_index = 1 + self.prompts = {} + + def register_prompt(self, prompt): + if prompt in self.prompts: + return '' + else: + index = self.next_index + self.next_index = index + 1 + self.prompts[prompt] = index + return """span.prompt%i:before { + content: "%s "; +} +""" % (index, prompt) + + def get_prompt_class(self, prompt): + return 'prompt%i' % self.prompts[prompt] + + +cache = PromptCache() + + class PromptDirective(rst.Directive): optional_arguments = 3 @@ -37,20 +66,15 @@ def run(self): prompts = prompt.split(',') html = '
'
-        html += ''
+                styles += cache.register_prompt(prompt)
+        if styles:
+            html += ''
         latex = "\\begin{Verbatim}[commandchars=\\\\\\{\\}]"
 
         Lexer = TextLexer
@@ -62,16 +86,16 @@ def run(self):
             Lexer = ScalaLexer
 
         statement = []
-        prompt_index = -1
-        for line in self.content:
-            if 'auto' in modifiers:
+        if 'auto' in modifiers:
+            prompt_class = ''
+            for line in self.content:
                 latex += '\n' + line
 
-                for index, prompt in enumerate(prompts):
+                for prompt in prompts:
                     if line.startswith(prompt):
                         if len(statement) > 0:
-                            html += '%s\n' % (
-                                prompt_index,
+                            html += '%s\n' % (
+                                prompt_class,
                                 highlight(
                                     '\n'.join(statement),
                                     Lexer(),
@@ -80,14 +104,26 @@ def run(self):
                             )
                             statement = []
                         line = line[len(prompt):].strip()
-                        prompt_index = index
+                        prompt_class = cache.get_prompt_class(prompt)
                         break
 
                 statement.append(line)
-            elif language == 'bash' or language == 'python':
+            # Add last prompt
+            if len(statement) > 0:
+                html += '%s\n' % (
+                    prompt_class,
+                    highlight(
+                        '\n'.join(statement),
+                        Lexer(),
+                        HtmlFormatter(nowrap=True)
+                    ).strip('\r\n')
+                )
+        elif language == 'bash' or language == 'python':
+            for line in self.content:
                 statement.append(line)
                 if len(line) == 0 or not line[-1] == '\\':
-                    html += '%s\n' % (
+                    html += '%s\n' % (
+                        cache.get_prompt_class(prompt),
                         highlight(
                             '\n'.join(statement),
                             Lexer(),
@@ -99,8 +135,10 @@ def run(self):
                     else:
                         latex += '\n' + '\n'.join(statement)
                     statement = []
-            else:
-                html += '%s\n' % (
+        else:
+            for line in self.content:
+                html += '%s\n' % (
+                    cache.get_prompt_class(prompt),
                     highlight(
                         line,
                         Lexer(),
@@ -112,17 +150,6 @@ def run(self):
                 else:
                     latex += '\n' + line
 
-        # Add last prompt
-        if 'auto' in modifiers and len(statement) > 0:
-            html += '%s\n' % (
-                prompt_index,
-                highlight(
-                    '\n'.join(statement),
-                    Lexer(),
-                    HtmlFormatter(nowrap=True)
-                ).strip('\r\n')
-            )
-
         html += "
" latex += "\n\\end{Verbatim}" @@ -134,3 +161,4 @@ def run(self): def setup(app): app.add_directive('prompt', PromptDirective) + app.connect('env-purge-doc', cache.clear)