Как я могу извлечь только начальное описание комментария Javadoc и игнорировать теги Javadoc, используя Python? - PullRequest
0 голосов
/ 15 апреля 2019

Я пытаюсь извлечь текст в Javadoc перед тегами Javadoc в python. До сих пор я был в состоянии избежать тега параметра, но есть и другие теги Javadoc, которые можно упомянуть сразу. Есть ли лучший способ сделать это?

parameterTag = "@param"
if (parameterTag in comments):
        splitComments = subsentence.split(my_string[my_string.find(start) + 1: my_string.find(parameterTag)])

Введите:


/**
     * Checks if the given node is inside the graph and
     * throws exception if the given node is null
     * @param a single node to be check
     * @return true if given node is contained in graph,
     *         return false otherwise
     * @requires given node != null
     */
    public boolean containsNode(E node){
        if(node==null){
            throw new IllegalArgumentException();
        }
        if(graph.containsKey(node)){
            return true;
        }
        return false;
    }

Выход:


/**
     * Checks if the given node is inside the graph and
     * throws exception if the given node is null
         */
    public boolean containsNode(E node){
        if(node==null){
            throw new IllegalArgumentException();
        }
        if(graph.containsKey(node)){
            return true;
        }
        return false;
    }

1 Ответ

0 голосов
/ 15 апреля 2019

Следуя вашей логике, есть часть описания , за которой следует часть "тегов" , а затем закрывающая отметка комментария .

Если построчно проверять наличие ключевого слова тега, вы не сможете справиться с этой строкой:

     *         return false otherwise

Следовательно, вам необходимо определить, вошли ли вы из выхода из части тегов. Ниже рабочий пример:

import re

# Javascript content loading
JSF = open("your_js_file.js", 'r')

js_content = JSF.read()

# We split the script into lines, and then we'll loop through the array
lineiterator = iter(js_content.splitlines())

# This boolean marks if we are or not in a "tags" part
in_tags = False

for line in lineiterator:
    # If we matched a line with a word starting with "@",
    # Then we entered into a "tags" section
    if re.search("@\w+", line) is not None :
        in_tags = True
    # If we matched a closing comment mark, then we left
    # The "tags" section (or never entered into it)
    if re.search("\*/",line) is not None:
        in_tags = False
    if not in_tags:
        print(line)
...