Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/nbconvert/utils/text.py: 33%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""Text related utils."""
3import os
4import re
7def indent(instr, nspaces=4, ntabs=0, flatten=False):
8 """Indent a string a given number of spaces or tabstops.
10 indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces.
12 Parameters
13 ----------
15 instr : basestring
16 The string to be indented.
17 nspaces : int (default: 4)
18 The number of spaces to be indented.
19 ntabs : int (default: 0)
20 The number of tabs to be indented.
21 flatten : bool (default: False)
22 Whether to scrub existing indentation. If True, all lines will be
23 aligned to the same indentation. If False, existing indentation will
24 be strictly increased.
26 Returns
27 -------
29 str|unicode : string indented by ntabs and nspaces.
31 """
32 if instr is None:
33 return None
34 ind = "\t" * ntabs + " " * nspaces
35 pat = re.compile("^\\s*", re.MULTILINE) if flatten else re.compile("^", re.MULTILINE)
36 outstr = re.sub(pat, ind, instr)
37 if outstr.endswith(os.linesep + ind):
38 return outstr[: -len(ind)]
39 return outstr