Coverage for /builds/ahmed.baizid.0/gtk-doc/gtkdoc/highlight.py: 21%

19 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2024-11-05 19:25 +0000

1#!/usr/bin/env python3 

2# -*- python; coding: utf-8 -*- 

3# 

4# gtk-doc - GTK DocBook documentation generator. 

5# Copyright (C) 2018 Stefan Sauer 

6# 

7# This program is free software; you can redistribute it and/or modify 

8# it under the terms of the GNU General Public License as published by 

9# the Free Software Foundation; either version 2 of the License, or 

10# (at your option) any later version. 

11# 

12# This program is distributed in the hope that it will be useful, 

13# but WITHOUT ANY WARRANTY; without even the implied warranty of 

14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

15# GNU General Public License for more details. 

16# 

17# You should have received a copy of the GNU General Public License 

18# along with this program; if not, write to the Free Software 

19# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 

20# 

21 

22""" 

23Highlight sourcecode snippets. 

24""" 

25import os 

26 

27from pygments import highlight 

28from pygments.lexers import CLexer 

29from pygments.lexers import get_lexer_by_name 

30from pygments.formatters import HtmlFormatter 

31 

32# lazily constructed lexer cache 

33LEXERS = { 

34 'c': CLexer() 

35} 

36HTML_FORMATTER = HtmlFormatter(nowrap=True) 

37 

38 

39def highlight_code(code, lang='c'): 

40 if lang not in LEXERS: 

41 LEXERS[lang] = get_lexer_by_name(lang) 

42 lexer = LEXERS.get(lang, None) 

43 if not lexer: 

44 return None 

45 return highlight(code, lexer, HTML_FORMATTER) 

46 

47 

48def append_style_defs(css_file_name): 

49 os.chmod(css_file_name, 0o644) 

50 with open(css_file_name, 'at', newline='\n', encoding='utf-8') as css: 

51 css.write(HTML_FORMATTER.get_style_defs())