Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

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

3# 

4# This file is part of the tsfpga project. 

5# https://tsfpga.com 

6# https://gitlab.com/tsfpga/tsfpga 

7# -------------------------------------------------------------------------------------------------- 

8 

9import re 

10 

11 

12class HtmlTranslator: 

13 

14 r""" 

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

16 

17 Supports: 

18 

19 * Strong: **double asterisks** 

20 * Emphasis: *single asterisks* 

21 

22 Literal asterisks should be escaped: \* 

23 """ 

24 

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

26 _double_asterisks = r"\*\*" 

27 _single_asterisk = r"\*" 

28 _match_text = r"(.*?)" 

29 

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

31 _re_strong_pattern = re.compile( 

32 _not_escaped + _double_asterisks + _match_text + _not_escaped + _double_asterisks 

33 ) 

34 _re_emphasis_pattern = re.compile( 

35 _not_escaped + _single_asterisk + _match_text + _not_escaped + _single_asterisk 

36 ) 

37 

38 # This pattern matches escaped asterisks 

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

40 

41 # Consecutive newlines is a paragraph separator 

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

43 

44 def translate(self, text): 

45 """ 

46 Translate the text to have HTML tags where appropriate. 

47 """ 

48 result = self._translate_angle_brackets(text) 

49 result = self._annotate(result) 

50 result = self._insert_line_breaks(result) 

51 return result 

52 

53 def _annotate(self, text): 

54 """ 

55 Replace markdown/rst syntax with HTML tags. 

56 """ 

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

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

59 # Remove the escape sign 

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

61 return result 

62 

63 def _insert_line_breaks(self, text): 

64 """ 

65 Insert HTML line break tag instead of consecutive newlines. 

66 """ 

67 # Two line breaks to get new paragraph. 

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

69 # A single newline in Markdown should be a space 

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

71 # Split to get nicer HTML file formatting 

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

73 return result 

74 

75 @staticmethod 

76 def _translate_angle_brackets(text): 

77 """ 

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

79 HTML tags by the web browse. 

80 """ 

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

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

83 return result