Coverage for src/lecture_citation/torst.py: 100%

21 statements  

« prev     ^ index     » next       coverage.py v7.1.0, created at 2023-08-18 01:58 +0200

1# -*- coding: utf-8 -*- 

2""" 

3@file 

4@brief Converts quotes into :epkg:`rst`. 

5""" 

6import textwrap 

7 

8 

9def to_rst(quote): 

10 """ 

11 Converts a quote defined as a dictionary 

12 into :epkg:`rst`. 

13 

14 @param quote dictionary 

15 @return text file 

16 """ 

17 rows = [".. quote::"] 

18 for k, v in quote.items(): 

19 if k != 'content': 

20 rows.append(" :{0}: {1}".format(k, v)) 

21 if 'content' not in quote: 

22 raise KeyError("Unable to kind key 'content'.") # pragma: no cover 

23 

24 content = quote['content'].replace("\n\n", '#LINE#').replace('\n', ' ') 

25 content = content.replace('\r', '').replace('\t', ' ') 

26 content = ' '.join(content.split()).strip() 

27 text = "\n".join(textwrap.wrap(content, 60)) 

28 text = text.replace("`", "").replace("\\textit", "") 

29 text = text.replace('{', '*').replace('}', '*') 

30 text = text.replace('#LINE#', '\n\n').strip("\n") 

31 text = textwrap.indent(text, ' ') 

32 text = text.replace(" - ", " — ") 

33 rows.append('') 

34 rows.append(text) 

35 rows.append('') 

36 return "\n".join(rows)