Coverage for hdl_registers/html_translator.py: 100%

30 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2022-09-29 22:03 +0000

1# -------------------------------------------------------------------------------------------------- 

2# Copyright (c) Lukas Vik. All rights reserved. 

3# 

4# This file is part of the hdl_registers project, a HDL register generator fast enough to be run 

5# in real time. 

6# https://hdl-registers.com 

7# https://gitlab.com/tsfpga/hdl_registers 

8# -------------------------------------------------------------------------------------------------- 

9 

10import re 

11 

12 

13class HtmlTranslator: 

14 

15 r""" 

16 Translate a raw text with markdown/rst annotations into HTML code. 

17 

18 Supports: 

19 

20 * Strong: **double asterisks** 

21 * Emphasis: *single asterisks* 

22 

23 Literal asterisks should be escaped: \* 

24 """ 

25 

26 _not_escaped = r"(?<!\\)" 

27 _double_asterisks = r"\*\*" 

28 _single_asterisk = r"\*" 

29 _match_text = r"(.*?)" 

30 

31 # These patterns match asterisks only if they are not preceded by \escape 

32 _re_strong_pattern = re.compile( 

33 _not_escaped + _double_asterisks + _match_text + _not_escaped + _double_asterisks 

34 ) 

35 _re_emphasis_pattern = re.compile( 

36 _not_escaped + _single_asterisk + _match_text + _not_escaped + _single_asterisk 

37 ) 

38 

39 # This pattern matches escaped asterisks 

40 _re_escaped_literal_pattern = re.compile(r"\\(\*)") 

41 

42 # Consecutive newlines is a paragraph separator 

43 _re_paragraph_separator = re.compile(r"\n{2,}") 

44 

45 def translate(self, text): 

46 """ 

47 Translate the text to have HTML tags where appropriate. 

48 """ 

49 result = self._translate_angle_brackets(text) 

50 result = self._annotate(result) 

51 result = self._insert_line_breaks(result) 

52 return result 

53 

54 def _annotate(self, text): 

55 """ 

56 Replace markdown/rst syntax with HTML tags. 

57 """ 

58 result = re.sub(self._re_strong_pattern, r"<strong>\g<1></strong>", text) 

59 result = re.sub(self._re_emphasis_pattern, r"<em>\g<1></em>", result) 

60 # Remove the escape sign 

61 result = re.sub(self._re_escaped_literal_pattern, r"\g<1>", result) 

62 return result 

63 

64 def _insert_line_breaks(self, text): 

65 """ 

66 Insert HTML line break tag instead of consecutive newlines. 

67 """ 

68 # Two line breaks to get new paragraph. 

69 result = re.sub(self._re_paragraph_separator, "<br /><br />", text) 

70 # A single newline in Markdown should be a space 

71 result = result.replace("\n", " ") 

72 # Split to get nicer HTML file formatting 

73 result = result.replace("<br />", "<br />\n") 

74 return result 

75 

76 @staticmethod 

77 def _translate_angle_brackets(text): 

78 """ 

79 The HTML may not contain raw angle brackets ("<", ">") since they will be interpreted as 

80 HTML tags by the web browse. 

81 """ 

82 result = text.replace("<", "&lt;") 

83 result = result.replace(">", "&gt;") 

84 return result