scdoc.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. """
  2. pygments.lexers.scdoc
  3. ~~~~~~~~~~~~~~~~~~~~~
  4. Lexer for scdoc, a simple man page generator.
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from pygments.lexer import RegexLexer, include, bygroups, using, this
  10. from pygments.token import Text, Comment, Keyword, String, Generic
  11. __all__ = ['ScdocLexer']
  12. class ScdocLexer(RegexLexer):
  13. """
  14. `scdoc` is a simple man page generator for POSIX systems written in C99.
  15. """
  16. name = 'scdoc'
  17. url = 'https://git.sr.ht/~sircmpwn/scdoc'
  18. aliases = ['scdoc', 'scd']
  19. filenames = ['*.scd', '*.scdoc']
  20. version_added = '2.5'
  21. flags = re.MULTILINE
  22. tokens = {
  23. 'root': [
  24. # comment
  25. (r'^(;.+\n)', bygroups(Comment)),
  26. # heading with pound prefix
  27. (r'^(#)([^#].+\n)', bygroups(Generic.Heading, Text)),
  28. (r'^(#{2})(.+\n)', bygroups(Generic.Subheading, Text)),
  29. # bulleted lists
  30. (r'^(\s*)([*-])(\s)(.+\n)',
  31. bygroups(Text, Keyword, Text, using(this, state='inline'))),
  32. # numbered lists
  33. (r'^(\s*)(\.+\.)( .+\n)',
  34. bygroups(Text, Keyword, using(this, state='inline'))),
  35. # quote
  36. (r'^(\s*>\s)(.+\n)', bygroups(Keyword, Generic.Emph)),
  37. # text block
  38. (r'^(```\n)([\w\W]*?)(^```$)', bygroups(String, Text, String)),
  39. include('inline'),
  40. ],
  41. 'inline': [
  42. # escape
  43. (r'\\.', Text),
  44. # underlines
  45. (r'(\s)(_[^_]+_)(\W|\n)', bygroups(Text, Generic.Emph, Text)),
  46. # bold
  47. (r'(\s)(\*[^*]+\*)(\W|\n)', bygroups(Text, Generic.Strong, Text)),
  48. # inline code
  49. (r'`[^`]+`', String.Backtick),
  50. # general text, must come last!
  51. (r'[^\\\s]+', Text),
  52. (r'.', Text),
  53. ],
  54. }
  55. def analyse_text(text):
  56. """We checks for bold and underline text with * and _. Also
  57. every scdoc file must start with a strictly defined first line."""
  58. result = 0
  59. if '*' in text:
  60. result += 0.01
  61. if '_' in text:
  62. result += 0.01
  63. # name(section) ["left_footer" ["center_header"]]
  64. first_line = text.partition('\n')[0]
  65. scdoc_preamble_pattern = r'^.*\([1-7]\)( "[^"]+"){0,2}$'
  66. if re.search(scdoc_preamble_pattern, first_line):
  67. result += 0.5
  68. return result