Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/nbconvert/utils/text.py: 27%

11 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-01 06:54 +0000

1"""Text related utils.""" 

2import os 

3import re 

4 

5 

6def indent(instr, nspaces=4, ntabs=0, flatten=False): 

7 """Indent a string a given number of spaces or tabstops. 

8 

9 indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces. 

10 

11 Parameters 

12 ---------- 

13 

14 instr : basestring 

15 The string to be indented. 

16 nspaces : int (default: 4) 

17 The number of spaces to be indented. 

18 ntabs : int (default: 0) 

19 The number of tabs to be indented. 

20 flatten : bool (default: False) 

21 Whether to scrub existing indentation. If True, all lines will be 

22 aligned to the same indentation. If False, existing indentation will 

23 be strictly increased. 

24 

25 Returns 

26 ------- 

27 

28 str|unicode : string indented by ntabs and nspaces. 

29 

30 """ 

31 if instr is None: 

32 return 

33 ind = "\t" * ntabs + " " * nspaces 

34 pat = re.compile("^\\s*", re.MULTILINE) if flatten else re.compile("^", re.MULTILINE) 

35 outstr = re.sub(pat, ind, instr) 

36 if outstr.endswith(os.linesep + ind): 

37 return outstr[: -len(ind)] 

38 else: 

39 return outstr