Esempio n. 1
0
def highlight_file(path):
    file_content = read_file(path)
    return highlight(
        file_content, JavaLexer(),
        HtmlFormatter(linenos=True, anchorlinenos=True, lineanchors="foo"))
Esempio n. 2
0
    def highlight_code(self, search_count, rank, targetpath):
        html_snippets = []
        matched_line_numbers = self.matched_line_number()

        if matched_line_numbers:
            snippet_cluster_lns = self.compute_lines_to_highlight(
                self.adjacent_line_numbers())
            snippets = []

            ##################### Data provision ######################
            pure_snippets_for_data_requirement = []  ##
            #snippet_cluster_lns		##lines_to_be_highlighted
            #self.file_content_lines	##entire code

            for snippet_cluster_ln in snippet_cluster_lns:
                snippet = []
                for n in snippet_cluster_ln:
                    snippet.append(self.file_content_lines[n])
                    pure_snippets_for_data_requirement.append(
                        self.file_content_lines[n])

                start_line = min(snippet_cluster_ln)
                # end_line = max(snippet_cluster_ln)
                highlight_lines = map(lambda x: x - start_line + 1,
                                      self.matching_line_numbers)
                snippets.append(
                    ("\n".join(snippet), start_line, highlight_lines))

            # #Data provision_ Defect4J (모든) 각 쿼리별 랭킹순으로 하이라이트 라인번호 + 전체코드 파일
            final_str = "\n".join(self.file_content_lines)
            original_project_list = [
                'knutwalker_google-closure-compiler',
                'google_closure-compiler', 'weitzj_closure-compiler',
                'jfree_jfreechart-fse', 'jfree_jfreechart',
                'apache_commons-math', 'apache_commons-lang',
                'mockito_mockito', 'bryceguo_mockito'
            ]

            if not self.file_path.split(
                    '/'
            )[6] in original_project_list:  #Check the duplicate projects.
                purepath = targetpath[:-4]
                if not os.path.exists(purepath):
                    os.makedirs(purepath)

                testcode_path = purepath + '_result_testcode'
                if not os.path.exists(testcode_path):
                    os.makedirs(testcode_path)

                if self.file_path.split(
                        '/')[-1] == 'Test.java':  # 결과 파일이 Test.java 인 것들 걸러내기
                    pass
                else:
                    final_path = purepath + "/" + str(rank) + '_' + str(
                        "||".join(self.file_path.split('/')[6:]))
                    write_file(final_path, str(final_str))
                    write_file(final_path + "_", str(snippet_cluster_lns))
                    print "*****************************", final_path, "is Done.."

                    # 여기서 test code 또한 찾아서 셋트로 돌려줘보자.
                    # >> 각각 결과코드에 해당하는 프로젝트 경로를 새로 다 뒤져서 현재 결과파일 앞뒤로 test 키워드 들어있는 파일들을 찾아본다.
                    # >> 테스트 파일 찾았으면 복붙

                    result_file_name = self.file_path.split('/')[-1]
                    result_pure_file_name = (
                        (self.file_path.split('/')[-1]).split('.')
                    )[0]  #pure name of the java file (e.g., ABC.java -> ABC)

                    stopwords = ['A', 'a', 'test']
                    javafiles = java_files_from_dir('/'.join(
                        self.file_path.split('/')[:7]))
                    for javafile in javafiles:
                        if result_pure_file_name in stopwords:
                            continue
                        name_of_javafile = javafile.split('/')[-1].split(
                            '.')[0]
                        if 'test' in name_of_javafile and result_pure_file_name in name_of_javafile:  #해당 결과파일의 이름과 'test' 라는 키워드가 들어가있는 파일이면 (e.g., xxxtest.java or testxxx.java)
                            content = read_file(javafile)
                            testcode_path = testcode_path + '/' + str(
                                rank) + '_' + str('||'.join(
                                    javafile.split('/')[6:]))
                            write_file(testcode_path, content)
                            write_file('/Users/Falcon/Desktop/count.txt',
                                       testcode_path)
                            write_file('/Users/Falcon/Desktop/count.txt',
                                       javafile)
                            write_file('/Users/Falcon/Desktop/count.txt',
                                       '**********************')

            html_snippets = [
                highlight(snippet[0], JavaLexer(),
                          LatexFormatter(linenos=True, linenostart=snippet[1]))
                for snippet in snippets
            ]
            self.code_snippets = [
                GitSearchItemSnippet(self.hl_snippet(snippet[0], snippet[1]),
                                     snippet[1]) for snippet in snippets
            ]

        if not html_snippets:
            html_snippets.append(
                highlight(self.file_content, JavaLexer(),
                          HtmlFormatter(linenos=True, anchorlinenos=True)))
            self.code_snippets.append(
                GitSearchItemSnippet(self.hl_snippet(self.file_content, 0), 0))
        return "".join(html_snippets)