@@ -727,85 +727,79 @@ def create_page() -> Tuple[Page, EmbeddedRstParser]:
727727 # Capture the original file-length before splicing it
728728 len_file = len (lines )
729729
730- if name == "literalinclude" :
731-
732- def _locate_text ( text : str ) -> int :
733- """
734- Searches the literally-included file ('lines') for the specified text. If no such text is found,
735- add an InvalidLiteralInclude diagnostic.
736- """
737- assert isinstance ( text , str )
738- loc = next (
739- ( idx for idx , line in enumerate ( lines ) if text in line ), - 1
730+ def _locate_text ( text : str ) -> int :
731+ """
732+ Searches the literally-included file ('lines') for the specified text. If no such text is found,
733+ add an InvalidLiteralInclude diagnostic.
734+ """
735+ assert isinstance ( text , str )
736+ loc = next (( idx for idx , line in enumerate ( lines ) if text in line ), - 1 )
737+ if loc < 0 :
738+ self . diagnostics . append (
739+ InvalidLiteralInclude ( f'" { text } " not found in { filepath } ' , line )
740740 )
741- if loc < 0 :
742- self .diagnostics .append (
743- InvalidLiteralInclude (
744- f'"{ text } " not found in { filepath } ' , line
745- )
746- )
747- return loc
748-
749- # Locate the start_after query
750- start_after = 0
751- if "start-after" in options :
752- start_after_text = options ["start-after" ]
753- # start_after = self._locate_text(start_after_text, lines, line, text)
754- start_after = _locate_text (start_after_text )
755- # Only increment start_after if text is specified, to avoid capturing the start_after_text
756- start_after += 1
757-
758- # ...now locate the end_before query
741+ return loc
742+
743+ # Locate the start_after query
744+ start_after = 0
745+ if "start-after" in options :
746+ start_after_text = options ["start-after" ]
747+ # start_after = self._locate_text(start_after_text, lines, line, text)
748+ start_after = _locate_text (start_after_text )
749+ # Only increment start_after if text is specified, to avoid capturing the start_after_text
750+ start_after += 1
751+
752+ # ...now locate the end_before query
753+ end_before = len (lines )
754+ if "end-before" in options :
755+ end_before_text = options ["end-before" ]
756+ # end_before = self._locate_text(end_before_text, lines, line, text)
757+ end_before = _locate_text (end_before_text )
758+
759+ # Check that start_after_text precedes end_before_text (and end_before exists)
760+ if start_after >= end_before >= 0 :
761+ self .diagnostics .append (
762+ InvalidLiteralInclude (
763+ f'"{ end_before_text } " precedes "{ start_after_text } " in { filepath } ' ,
764+ line ,
765+ )
766+ )
767+
768+ # If we failed to locate end_before text, default to the end-of-file
769+ if end_before == - 1 :
759770 end_before = len (lines )
760- if "end-before" in options :
761- end_before_text = options ["end-before" ]
762- # end_before = self._locate_text(end_before_text, lines, line, text)
763- end_before = _locate_text (end_before_text )
764771
765- # Check that start_after_text precedes end_before_text (and end_before exists)
766- if start_after >= end_before >= 0 :
772+ lines = lines [start_after :end_before ]
773+
774+ dedent = 0
775+ if "dedent" in options :
776+ # Dedent is specified as a flag
777+ if isinstance (options ["dedent" ], bool ):
778+ # Deduce a reasonable dedent
779+ try :
780+ dedent = min (
781+ len (line ) - len (line .lstrip ())
782+ for line in lines
783+ if len (line .lstrip ()) > 0
784+ )
785+ except ValueError :
786+ # Handle the (unlikely) case where there are no non-empty lines
787+ dedent = 0
788+ # Dedent is specified as a nonnegative integer (number of characters):
789+ # Note: since boolean is a subtype of int, this conditonal must follow the
790+ # above bool-type conditional.
791+ elif isinstance (options ["dedent" ], int ):
792+ dedent = options ["dedent" ]
793+ else :
767794 self .diagnostics .append (
768795 InvalidLiteralInclude (
769- f'" { end_before_text } " precedes " { start_after_text } " in { filepath } ' ,
796+ f'Dedent " { dedent } " of type { type ( dedent ) } ; expected nonnegative integer or flag ' ,
770797 line ,
771798 )
772799 )
800+ return doc
773801
774- # If we failed to locate end_before text, default to the end-of-file
775- if end_before == - 1 :
776- end_before = len (lines )
777-
778- lines = lines [start_after :end_before ]
779-
780- dedent = 0
781- if "dedent" in options :
782- # Dedent is specified as a flag
783- if isinstance (options ["dedent" ], bool ):
784- # Deduce a reasonable dedent
785- try :
786- dedent = min (
787- len (line ) - len (line .lstrip ())
788- for line in lines
789- if len (line .lstrip ()) > 0
790- )
791- except ValueError :
792- # Handle the (unlikely) case where there are no non-empty lines
793- dedent = 0
794- # Dedent is specified as a nonnegative integer (number of characters):
795- # Note: since boolean is a subtype of int, this conditonal must follow the
796- # above bool-type conditional.
797- elif isinstance (options ["dedent" ], int ):
798- dedent = options ["dedent" ]
799- else :
800- self .diagnostics .append (
801- InvalidLiteralInclude (
802- f'Dedent "{ dedent } " of type { type (dedent )} ; expected nonnegative integer or flag' ,
803- line ,
804- )
805- )
806- return doc
807-
808- lines = [line [dedent :] for line in lines ]
802+ lines = [line [dedent :] for line in lines ]
809803
810804 emphasize_lines = None
811805 if "emphasize-lines" in options :
0 commit comments