scdoc.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. """
  2. pygments.lexers.scdoc
  3. ~~~~~~~~~~~~~~~~~~~~~
  4. Lexer for scdoc, a simple man page generator.
  5. :copyright: Copyright 2006-2021 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, \
  10. using, this
  11. from pygments.token import Text, Comment, Keyword, String, \
  12. Generic
  13. __all__ = ['ScdocLexer']
  14. class ScdocLexer(RegexLexer):
  15. """
  16. `scdoc` is a simple man page generator for POSIX systems written in C99.
  17. https://git.sr.ht/~sircmpwn/scdoc
  18. .. versionadded:: 2.5
  19. """
  20. name = 'scdoc'
  21. aliases = ['scdoc', 'scd']
  22. filenames = ['*.scd', '*.scdoc']
  23. flags = re.MULTILINE
  24. tokens = {
  25. 'root': [
  26. # comment
  27. (r'^(;.+\n)', bygroups(Comment)),
  28. # heading with pound prefix
  29. (r'^(#)([^#].+\n)', bygroups(Generic.Heading, Text)),
  30. (r'^(#{2})(.+\n)', bygroups(Generic.Subheading, Text)),
  31. # bulleted lists
  32. (r'^(\s*)([*-])(\s)(.+\n)',
  33. bygroups(Text, Keyword, Text, using(this, state='inline'))),
  34. # numbered lists
  35. (r'^(\s*)(\.+\.)( .+\n)',
  36. bygroups(Text, Keyword, using(this, state='inline'))),
  37. # quote
  38. (r'^(\s*>\s)(.+\n)', bygroups(Keyword, Generic.Emph)),
  39. # text block
  40. (r'^(```\n)([\w\W]*?)(^```$)', bygroups(String, Text, String)),
  41. include('inline'),
  42. ],
  43. 'inline': [
  44. # escape
  45. (r'\\.', Text),
  46. # underlines
  47. (r'(\s)(_[^_]+_)(\W|\n)', bygroups(Text, Generic.Emph, Text)),
  48. # bold
  49. (r'(\s)(\*[^*]+\*)(\W|\n)', bygroups(Text, Generic.Strong, Text)),
  50. # inline code
  51. (r'`[^`]+`', String.Backtick),
  52. # general text, must come last!
  53. (r'[^\\\s]+', Text),
  54. (r'.', Text),
  55. ],
  56. }
  57. def analyse_text(text):
  58. """This is very similar to markdown, save for the escape characters
  59. needed for * and _."""
  60. result = 0
  61. if '\\*' in text:
  62. result += 0.01
  63. if '\\_' in text:
  64. result += 0.01
  65. return result