You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.7KB

  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from __future__ import print_function
  4. help_text = """
  5. Extracts tags from rmd files. Useful for the Tagbar plugin.
  6. Usage:
  7. Install Tagbar (http://majutsushi.github.io/tagbar/). Then, put this file
  8. anywhere and add the following to your .vimrc:
  9. let g:tagbar_type_rmd = {
  10. \ 'ctagstype':'rmd'
  11. \ , 'kinds':['h:header', 'c:chunk', 'f:function', 'v:variable']
  12. \ , 'sro':'&&&'
  13. \ , 'kind2scope':{'h':'header', 'c':'chunk', 'f':'function', 'v':'variable'}
  14. \ , 'sort':0
  15. \ , 'ctagsbin':'/path/to/rmdtags.py'
  16. \ . 'ctagsargs':''
  17. \ }
  18. """
  19. import sys
  20. import re
  21. if len(sys.argv) < 2:
  22. print(help_text)
  23. exit()
  24. filename = sys.argv[1]
  25. print(filename)
  26. re_header = re.compile(r"^(#+)([^{]+)(\{[^}]+\})?$")
  27. re_chunk = re.compile(r"^```\{r ([^,]+)(,[^}]*)?\}$")
  28. re_function = re.compile(r"^[\t ]*([^ ]+) *<- *function.*$")
  29. # re_variable1 = re.compile(r"^[\t ]*([^ ]+) *<-.*$")
  30. # re_variable2 = re.compile(r"^.*-> *(.+)$")
  31. file_content = []
  32. try:
  33. with open(filename, "r") as vim_buffer:
  34. file_content = vim_buffer.readlines()
  35. except:
  36. exit()
  37. headers = []
  38. depth = [0]
  39. inChunk = False
  40. curChunk = None
  41. for lnum, line in enumerate(file_content):
  42. newline = []
  43. tag = ""
  44. signature = ""
  45. options = ""
  46. # Headers
  47. if re_header.match(line) and not inChunk:
  48. level = len(re_header.match(line).group(1))
  49. tag = re_header.match(line).group(2).strip()
  50. newline.append(tag)
  51. newline.append(filename)
  52. newline.append('/^' + line.rstrip("\n") + '$/;"')
  53. newline.append("h")
  54. newline.append("line:" + str(lnum+1))
  55. # header
  56. while (level <= depth[len(depth) -1]):
  57. headers.pop()
  58. depth.pop()
  59. if len(headers) > 0:
  60. options = "header:" + "&&&".join(headers)
  61. headers.append(tag)
  62. depth.append(level)
  63. # signature
  64. if re_header.match(line).group(3):
  65. signature = "(" + re_header.match(line).group(3).strip("{}") + ")"
  66. options = options + "\tsignature:" + signature
  67. # Print
  68. newline.append(options)
  69. print("\t".join(newline))
  70. # Chunks
  71. if re_chunk.match(line):
  72. inChunk = True
  73. curChunk = re_chunk.match(line).group(1).strip()
  74. newline.append(re_chunk.match(line).group(1).strip())
  75. newline.append(filename)
  76. newline.append('/^' + line.rstrip("\n") + '$/;"')
  77. newline.append("c")
  78. newline.append("line:" + str(lnum+1))
  79. if len(headers) > 0:
  80. options = "header:" + "&&&".join(headers)
  81. if re_chunk.match(line).group(2):
  82. signature = "(" + re_chunk.match(line).group(2).lstrip(", ") + ")"
  83. options = options + "\tsignature:" + signature
  84. # Print
  85. newline.append(options)
  86. print("\t".join(newline))
  87. if line == "```\n":
  88. inChunk = False
  89. curChunk = None
  90. # Functions
  91. if re_function.match(line) and inChunk:
  92. newline.append(re_function.match(line).group(1).strip())
  93. newline.append(filename)
  94. newline.append('/^' + line.rstrip("\n") + '$/;"')
  95. newline.append("f")
  96. newline.append("line:" + str(lnum+1))
  97. print("\t".join(newline))
  98. # elif (re_variable1.match(line) or re_variable2.match(line)) and inChunk:
  99. # tag = re_variable1.match(line) or re_variable2.match(line)
  100. # tag = tag.group(1).strip()
  101. # newline.append(tag)
  102. # newline.append(filename)
  103. # newline.append('/^' + line.rstrip("\n") + '$/;"')
  104. # newline.append("v")
  105. # newline.append("line:" + str(lnum+1))
  106. # print("\t".join(newline))