/env/Lib/site-packages/wx-2.8-msw-unicode/wx/tools/Editra/src/syntax/_django.py

https://bitbucket.org/beqa/nvdadependencyvirtualenvironment · Python · 141 lines · 77 code · 24 blank · 40 comment · 7 complexity · d17eca50910dd600bdc623c650ea825b MD5 · raw file

  1. ###############################################################################
  2. # Name: django.py #
  3. # Purpose: Define Django syntax for highlighting and other features #
  4. # Author: Cody Precord <cprecord@editra.org> #
  5. # Copyright: (c) 2008 Cody Precord <staff@editra.org> #
  6. # License: wxWindows License #
  7. ###############################################################################
  8. """
  9. FILE: django.py
  10. AUTHOR: Cody Precord
  11. @summary: Lexer configuration module for Django Templates.
  12. """
  13. __author__ = "Cody Precord <cprecord@editra.org>"
  14. __svnid__ = "$Id: _django.py 65146 2010-07-31 05:25:02Z CJP $"
  15. __revision__ = "$Revision: 65146 $"
  16. #-----------------------------------------------------------------------------#
  17. # Imports
  18. import wx.stc as stc
  19. from pygments.token import Token
  20. from pygments.lexers import get_lexer_by_name
  21. #Local Imports
  22. import synglob
  23. import syndata
  24. #-----------------------------------------------------------------------------#
  25. # Style Id's
  26. STC_DJANGO_DEFAULT, \
  27. STC_DJANGO_COMMENT, \
  28. STC_DJANGO_NUMBER, \
  29. STC_DJANGO_STRING, \
  30. STC_DJANGO_STRINGEOL, \
  31. STC_DJANGO_SCALAR, \
  32. STC_DJANGO_OPERATOR, \
  33. STC_DJANGO_PREPROCESSOR, \
  34. STC_DJANGO_ATTRIBUTE, \
  35. STC_DJANGO_TAG, \
  36. STC_DJANGO_BUILTIN, \
  37. STC_DJANGO_KEYWORD = range(12)
  38. #-----------------------------------------------------------------------------#
  39. # Python Keywords
  40. KEYWORDS = ("true false undefined null in as reversed recursive not and or is "
  41. "if else import with loop block forloop")
  42. #---- Syntax Style Specs ----#
  43. SYNTAX_ITEMS = [ (STC_DJANGO_DEFAULT, 'default_style'),
  44. (STC_DJANGO_COMMENT, 'comment_style'),
  45. (STC_DJANGO_NUMBER, 'number_style'),
  46. (STC_DJANGO_STRING, 'string_style'),
  47. (STC_DJANGO_STRINGEOL, 'stringeol_style'),
  48. (STC_DJANGO_SCALAR, 'scalar_style'),
  49. (STC_DJANGO_OPERATOR, 'operator_style'),
  50. (STC_DJANGO_PREPROCESSOR, 'pre_style'),
  51. (STC_DJANGO_ATTRIBUTE, 'keyword2_style'),
  52. (STC_DJANGO_TAG, 'keyword_style'), # Need new tag
  53. (STC_DJANGO_BUILTIN, 'keyword4_style'),
  54. (STC_DJANGO_KEYWORD, 'keyword_style'), ]
  55. #-----------------------------------------------------------------------------#
  56. class SyntaxData(syndata.SyntaxDataBase):
  57. """SyntaxData object for Django"""
  58. def __init__(self, langid):
  59. syndata.SyntaxDataBase.__init__(self, langid)
  60. # Setup
  61. self.SetLexer(stc.STC_LEX_CONTAINER)
  62. self.RegisterFeature(synglob.FEATURE_STYLETEXT, StyleText)
  63. def GetKeywords(self):
  64. """Returns Specified Keywords List """
  65. return [(1, KEYWORDS)]
  66. def GetSyntaxSpec(self):
  67. """Syntax Specifications """
  68. return SYNTAX_ITEMS
  69. def GetCommentPattern(self):
  70. """Returns a list of characters used to comment a block of code """
  71. return [u"#",]
  72. #-----------------------------------------------------------------------------#
  73. def StyleText(stc, start, end):
  74. """Style the text
  75. @param stc: Styled text control instance
  76. @param start: Start position
  77. @param end: end position
  78. """
  79. cpos = 0
  80. stc.StartStyling(cpos, 0x1f)
  81. lexer = get_lexer_by_name("html+django")
  82. doctxt = stc.GetTextRange(0, end)
  83. wineol = stc.GetEOLChar() == "\r\n"
  84. # Need to convert to UTF-8 in order to calculate
  85. # correct positions in STC.
  86. try:
  87. doctxt = doctxt.encode('utf-8')
  88. except:
  89. pass
  90. for token, txt in lexer.get_tokens(doctxt):
  91. # print token, txt
  92. style = TOKEN_MAP.get(token, STC_DJANGO_DEFAULT)
  93. if style == STC_DJANGO_PREPROCESSOR and txt.startswith(u'#'):
  94. style = STC_DJANGO_COMMENT
  95. # elif style == STC_DJANGO_STRING and txt[-1] not in '"\'':
  96. # style = STC_DJANGO_STRINGEOL
  97. tlen = len(txt)
  98. if wineol and "\n" in txt:
  99. tlen += txt.count("\n")
  100. if tlen:
  101. stc.SetStyling(tlen, style)
  102. cpos += tlen
  103. stc.StartStyling(cpos, 0x1f)
  104. #-----------------------------------------------------------------------------#
  105. TOKEN_MAP = { Token.Literal.String : STC_DJANGO_STRING,
  106. Token.Comment.Preproc : STC_DJANGO_PREPROCESSOR,
  107. Token.Comment : STC_DJANGO_COMMENT,
  108. Token.Name.Builtin : STC_DJANGO_BUILTIN,
  109. Token.Operator : STC_DJANGO_OPERATOR,
  110. Token.Punctuation : STC_DJANGO_OPERATOR,
  111. Token.Number : STC_DJANGO_NUMBER,
  112. Token.Keyword : STC_DJANGO_KEYWORD,
  113. Token.Name.Attribute : STC_DJANGO_ATTRIBUTE,
  114. Token.String.Interpol : STC_DJANGO_SCALAR,
  115. Token.Name.Tag : STC_DJANGO_TAG }