11"""
2- sphinxnotes.snippet
3- ~~~~~~~~~~~~~~~~~~~
2+ sphinxnotes.snippet
3+ ~~~~~~~~~~~~~~~~~~~
44
5- :copyright: Copyright 2020 Shengyu Zhang
6- :license: BSD, see LICENSE for details.
5+ :copyright: Copyright 2020 Shengyu Zhang
6+ :license: BSD, see LICENSE for details.
77"""
88
99from __future__ import annotations
1414
1515__version__ = '1.1.1'
1616
17+
1718class Snippet (object ):
1819 """
1920 Snippet is base class of reStructuredText snippet.
2021
21- :param nodes: Document nodes that make up this snippet
22+ :param nodes: Document nodes that make up this snippet
2223 """
2324
2425 #: Source file path of snippet
25- file :str
26+ file : str
2627
2728 #: Line number range of snippet, in the source file which is left closed
2829 #: and right opened.
29- lineno :Tuple [int ,int ]
30+ lineno : Tuple [int , int ]
3031
3132 #: The original reStructuredText of snippet
32- rst :List [str ]
33+ rst : List [str ]
3334
3435 #: The possible identifier key of snippet, which is picked from nodes'
3536 #: (or nodes' parent's) `ids attr`_.
3637 #:
3738 #: .. _ids attr: https://docutils.sourceforge.io/docs/ref/doctree.html#ids
38- refid :Optional [str ]
39+ refid : Optional [str ]
3940
40- def __init__ (self , * nodes :nodes .Node ) -> None :
41+ def __init__ (self , * nodes : nodes .Node ) -> None :
4142 assert len (nodes ) != 0
4243
4344 self .file = nodes [0 ].source
4445
4546 lineno = [float ('inf' ), - float ('inf' )]
4647 for node in nodes :
4748 if not node .line :
48- continue # Skip node that have None line, I dont know why
49+ continue # Skip node that have None line, I dont know why
4950 lineno [0 ] = min (lineno [0 ], _line_of_start (node ))
5051 lineno [1 ] = max (lineno [1 ], _line_of_end (node ))
5152 self .lineno = lineno
5253
5354 lines = []
54- with open (self .file , "r" ) as f :
55+ with open (self .file , 'r' ) as f :
5556 start = self .lineno [0 ] - 1
5657 stop = self .lineno [1 ] - 1
5758 for line in itertools .islice (f , start , stop ):
@@ -73,61 +74,60 @@ def __init__(self, *nodes:nodes.Node) -> None:
7374 break
7475
7576
76-
7777class Text (Snippet ):
7878 #: Text of snippet
79- text :str
79+ text : str
8080
81- def __init__ (self , node :nodes .Node ) -> None :
81+ def __init__ (self , node : nodes .Node ) -> None :
8282 super ().__init__ (node )
8383 self .text = node .astext ()
8484
8585
8686class CodeBlock (Text ):
8787 #: Language of code block
88- language :str
88+ language : str
8989 #: Caption of code block
90- caption :Optional [str ]
90+ caption : Optional [str ]
9191
92- def __init__ (self , node :nodes .literal_block ) -> None :
92+ def __init__ (self , node : nodes .literal_block ) -> None :
9393 assert isinstance (node , nodes .literal_block )
9494 super ().__init__ (node )
9595 self .language = node ['language' ]
9696 self .caption = node .get ('caption' )
9797
9898
9999class WithCodeBlock (object ):
100- code_blocks :List [CodeBlock ]
100+ code_blocks : List [CodeBlock ]
101101
102- def __init__ (self , nodes :nodes .Nodes ) -> None :
102+ def __init__ (self , nodes : nodes .Nodes ) -> None :
103103 self .code_blocks = []
104104 for n in nodes .traverse (nodes .literal_block ):
105105 self .code_blocks .append (self .CodeBlock (n ))
106106
107107
108108class Title (Text ):
109- def __init__ (self , node :nodes .title ) -> None :
109+ def __init__ (self , node : nodes .title ) -> None :
110110 assert isinstance (node , nodes .title )
111111 super ().__init__ (node )
112112
113113
114114class WithTitle (object ):
115- title :Optional [Title ]
115+ title : Optional [Title ]
116116
117- def __init__ (self , node :nodes .Node ) -> None :
117+ def __init__ (self , node : nodes .Node ) -> None :
118118 title_node = node .next_node (nodes .title )
119119 self .title = Title (title_node ) if title_node else None
120120
121121
122122class Section (Snippet , WithTitle ):
123- def __init__ (self , node :nodes .section ) -> None :
123+ def __init__ (self , node : nodes .section ) -> None :
124124 assert isinstance (node , nodes .section )
125125 Snippet .__init__ (self , node )
126126 WithTitle .__init__ (self , node )
127127
128128
129129class Document (Section ):
130- def __init__ (self , node :nodes .document ) -> None :
130+ def __init__ (self , node : nodes .document ) -> None :
131131 assert isinstance (node , nodes .document )
132132 super ().__init__ (node .next_node (nodes .section ))
133133
@@ -136,7 +136,8 @@ def __init__(self, node:nodes.document) -> None:
136136# Nodes helper #
137137################
138138
139- def _line_of_start (node :nodes .Node ) -> int :
139+
140+ def _line_of_start (node : nodes .Node ) -> int :
140141 assert node .line
141142 if isinstance (node , nodes .title ):
142143 if isinstance (node .parent .parent , nodes .document ):
@@ -155,7 +156,7 @@ def _line_of_start(node:nodes.Node) -> int:
155156 return node .line
156157
157158
158- def _line_of_end (node :nodes .Node ) -> Optional [int ]:
159+ def _line_of_end (node : nodes .Node ) -> Optional [int ]:
159160 next_node = node .next_node (descend = False , siblings = True , ascend = True )
160161 while next_node :
161162 if next_node .line :
@@ -166,7 +167,9 @@ def _line_of_end(node:nodes.Node) -> Optional[int]:
166167 descend = True ,
167168 # If node and its children have not valid line attr, try use line
168169 # of next node
169- ascend = True , siblings = True )
170+ ascend = True ,
171+ siblings = True ,
172+ )
170173 # No line found, return the max line of source file
171174 if node .source :
172175 with open (node .source ) as f :
0 commit comments