25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

101 satır
2.6 KiB

  1. """
  2. pygments.lexers.jsx
  3. ~~~~~~~~~~~~~~~~~~~
  4. Lexers for JSX (React) and TSX (TypeScript flavor).
  5. :copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from pygments.lexer import bygroups, default, include, inherit
  10. from pygments.lexers.javascript import JavascriptLexer, TypeScriptLexer
  11. from pygments.token import Name, Operator, Punctuation, String, Text, \
  12. Whitespace
  13. __all__ = ['JsxLexer', 'TsxLexer']
  14. _JSX_RULES = {
  15. "jsx": [
  16. (r"</?>", Punctuation), # JSXFragment <>|</>
  17. (r"(<)(\w+)(\.?)", bygroups(Punctuation, Name.Tag, Punctuation), "tag"),
  18. (
  19. r"(</)(\w+)(>)",
  20. bygroups(Punctuation, Name.Tag, Punctuation),
  21. ),
  22. (
  23. r"(</)(\w+)",
  24. bygroups(Punctuation, Name.Tag),
  25. "fragment",
  26. ), # Same for React.Context
  27. ],
  28. "tag": [
  29. (r"\s+", Whitespace),
  30. (r"([\w-]+)(\s*)(=)(\s*)", bygroups(Name.Attribute, Whitespace, Operator, Whitespace), "attr"),
  31. (r"[{}]+", Punctuation),
  32. (r"[\w\.]+", Name.Attribute),
  33. (r"(/?)(\s*)(>)", bygroups(Punctuation, Text, Punctuation), "#pop"),
  34. ],
  35. "fragment": [
  36. (r"(.)(\w+)", bygroups(Punctuation, Name.Attribute)),
  37. (r"(>)", bygroups(Punctuation), "#pop"),
  38. ],
  39. "attr": [
  40. (r"\{", Punctuation, "expression"),
  41. (r'".*?"', String, "#pop"),
  42. (r"'.*?'", String, "#pop"),
  43. default("#pop"),
  44. ],
  45. "expression": [
  46. (r"\{", Punctuation, "#push"),
  47. (r"\}", Punctuation, "#pop"),
  48. include("root"),
  49. ],
  50. }
  51. class JsxLexer(JavascriptLexer):
  52. """For JavaScript Syntax Extension (JSX).
  53. """
  54. name = "JSX"
  55. aliases = ["jsx", "react"]
  56. filenames = ["*.jsx", "*.react"]
  57. mimetypes = ["text/jsx", "text/typescript-jsx"]
  58. url = "https://facebook.github.io/jsx/"
  59. version_added = '2.17'
  60. flags = re.MULTILINE | re.DOTALL
  61. # Use same tokens as `JavascriptLexer`, but with tags and attributes support
  62. tokens = {
  63. "root": [
  64. include("jsx"),
  65. inherit,
  66. ],
  67. **_JSX_RULES}
  68. class TsxLexer(TypeScriptLexer):
  69. """For TypeScript with embedded JSX
  70. """
  71. name = "TSX"
  72. aliases = ["tsx"]
  73. filenames = ["*.tsx"]
  74. mimetypes = ["text/typescript-tsx"]
  75. url = "https://www.typescriptlang.org/docs/handbook/jsx.html"
  76. version_added = '2.19'
  77. flags = re.MULTILINE | re.DOTALL
  78. # Use same tokens as `TypescriptLexer`, but with tags and attributes support
  79. tokens = {
  80. "root": [
  81. include("jsx"),
  82. inherit,
  83. ],
  84. **_JSX_RULES}