|
| 1 | +""" |
| 2 | + tutorialformatter |
| 3 | + =========================== |
| 4 | +
|
| 5 | + This extension provides a directive to include a source code file |
| 6 | + in a document, but with certain comments from the file formatted |
| 7 | + as regular document text. This allows code for a tutorial to look like: |
| 8 | +
|
| 9 | + /// BEGIN_TUTORIAL |
| 10 | + /// This next line adds one. |
| 11 | + i = i + 1; |
| 12 | + /// Then we need to double it. |
| 13 | + i = i * 2; |
| 14 | + /// END_TUTORIAL |
| 15 | +
|
| 16 | + And have it formatted as |
| 17 | +
|
| 18 | + This next line adds one.:: |
| 19 | + i = i + 1; |
| 20 | +
|
| 21 | + Then we need to double it.:: |
| 22 | + i = i * 2; |
| 23 | +
|
| 24 | + The special-looking comment character sequence at the start of |
| 25 | + each text line can be anything not starting or ending with |
| 26 | + whitespace. tutorialformatter starts by scanning the file for the |
| 27 | + string BEGIN_TUTORIAL. When it finds it, it takes all the |
| 28 | + characters before BEGIN_TUTORIAL on that line, strips whitespace |
| 29 | + from the left, and uses that as the text marker. So this would |
| 30 | + also be fine: |
| 31 | +
|
| 32 | + #My Tutorial# BEGIN_TUTORIAL |
| 33 | + #My Tutorial# This next line adds one. |
| 34 | + i = i + 1 |
| 35 | + #My Tutorial# Then we need to double it. |
| 36 | + i = i * 2 |
| 37 | + #My Tutorial# END_TUTORIAL |
| 38 | +
|
| 39 | + .. moduleauthor:: Dave Hershberger <[email protected]> |
| 40 | +""" |
| 41 | + |
| 42 | +__version__ = '0.1.0' |
| 43 | + |
| 44 | +import os |
| 45 | +from docutils.parsers import rst |
| 46 | +from docutils.parsers.rst.directives import flag, unchanged |
| 47 | +from docutils.statemachine import string2lines |
| 48 | +from pygments.lexers import get_lexer_for_filename |
| 49 | + |
| 50 | +class TutorialFormatterDirective(rst.Directive): |
| 51 | + has_content = False |
| 52 | + final_argument_whitespace = True |
| 53 | + required_arguments = 1 |
| 54 | + |
| 55 | + option_spec = dict(shell=flag, prompt=flag, nostderr=flag, |
| 56 | + in_srcdir=flag, extraargs=unchanged, |
| 57 | + until=unchanged) |
| 58 | + |
| 59 | + def run(self): |
| 60 | + filename = self.arguments[0] |
| 61 | + text_tag = None |
| 62 | + tag_len = 0 |
| 63 | + |
| 64 | + filepath = self.state.document.settings.env.srcdir |
| 65 | + absfilename = os.path.join( filepath, filename ) |
| 66 | + if absfilename.endswith('.h'): |
| 67 | + language = 'c++' |
| 68 | + elif absfilename.endswith('CMakeLists.txt'): |
| 69 | + language = 'cmake' |
| 70 | + else: |
| 71 | + try: |
| 72 | + language = get_lexer_for_filename( absfilename ).name.lower() |
| 73 | + if language == 'text only': |
| 74 | + language = 'none' |
| 75 | + except: |
| 76 | + language = 'none' |
| 77 | + code_prefix = '\n.. code-block:: ' + language + '\n\n' |
| 78 | + code_suffix = '\n' |
| 79 | + |
| 80 | + print "tutorial-formatter running on", absfilename |
| 81 | + file_ = open( absfilename, 'r' ) |
| 82 | + text_to_process = "" |
| 83 | + current_block = "" |
| 84 | + in_code = False |
| 85 | + in_text = False |
| 86 | + in_tutorial = False |
| 87 | + for line in file_: |
| 88 | + if not in_tutorial: |
| 89 | + begin_pos = line.find( 'BEGIN_TUTORIAL' ) |
| 90 | + if begin_pos != -1: |
| 91 | + text_tag = line[:begin_pos].lstrip() |
| 92 | + tag_len = len( text_tag ) |
| 93 | + in_tutorial = True |
| 94 | + continue |
| 95 | + if line.find( 'END_TUTORIAL' ) != -1: |
| 96 | + break |
| 97 | + stripped = line.lstrip() |
| 98 | + if stripped.startswith( text_tag.strip() ): |
| 99 | + if in_code: |
| 100 | + text_to_process += code_prefix + current_block + code_suffix |
| 101 | + current_block = "" |
| 102 | + in_code = False |
| 103 | + in_text = True |
| 104 | + addition = stripped[tag_len:] |
| 105 | + if addition == '' or addition[-1] != '\n': |
| 106 | + addition += '\n' |
| 107 | + current_block += addition |
| 108 | + else: |
| 109 | + if in_text: |
| 110 | + text_to_process += current_block |
| 111 | + current_block = "" |
| 112 | + in_text = False |
| 113 | + in_code = True # Code to show begins right after tagged text |
| 114 | + if in_code: |
| 115 | + current_block += ' ' + line |
| 116 | + if in_code: |
| 117 | + text_to_process += code_prefix + current_block + code_suffix |
| 118 | + elif in_text: |
| 119 | + text_to_process += current_block |
| 120 | + |
| 121 | + # Debug writes... |
| 122 | + #print 'text_to_process =' |
| 123 | + #print text_to_process |
| 124 | + #print '= text_to_process' |
| 125 | + |
| 126 | + lines = string2lines( text_to_process ) |
| 127 | + self.state_machine.insert_input( lines, absfilename ) |
| 128 | + |
| 129 | + return [] |
| 130 | + |
| 131 | +def setup(app): |
| 132 | + app.add_directive('tutorial-formatter', TutorialFormatterDirective) |
0 commit comments