2012-05-09 12:14:25 +02:00
|
|
|
# coding: utf-8
|
|
|
|
#
|
2015-08-24 17:23:54 +02:00
|
|
|
# Copyright © 2012-2014 Ejwa Software. All rights reserved.
|
2012-05-09 12:14:25 +02:00
|
|
|
#
|
|
|
|
# This file is part of gitinspector.
|
|
|
|
#
|
|
|
|
# gitinspector is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# gitinspector is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with gitinspector. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2013-06-25 16:37:18 +02:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2014-12-04 16:15:09 +01:00
|
|
|
__comment_begining__ = {"java": "/*", "c": "/*", "cc": "/*", "cpp": "/*", "h": "/*", "hh": "/*", "hpp": "/*", "hs": "{-", "html": "<!--",
|
2013-12-05 08:47:20 +01:00
|
|
|
"php": "/*", "py": "\"\"\"", "glsl": "/*", "rb": "=begin", "js": "/*", "jspx": "<!--", "scala": "/*",
|
2014-11-02 22:35:54 +01:00
|
|
|
"sql": "/*", "tex": "\\begin{comment}", "xhtml": "<!--", "xml": "<!--", "ml": "(*", "mli": "(*" }
|
2012-05-09 12:14:25 +02:00
|
|
|
|
2014-12-04 16:15:09 +01:00
|
|
|
__comment_end__ = {"java": "*/", "c": "*/", "cc": "*/", "cpp": "*/", "h": "*/", "hh": "*/", "hpp": "*/", "hs": "-}", "html": "-->",
|
2013-12-05 08:47:20 +01:00
|
|
|
"php": "/*", "py": "\"\"\"", "glsl": "*/", "rb": "=end", "js": "*/", "jspx": "-->", "scala": "*/",
|
2014-11-02 22:35:54 +01:00
|
|
|
"sql": "*/", "tex": "\\end{comment}", "xhtml": "-->", "xml": "-->", "ml": "*)", "mli": "*)" }
|
2012-05-09 12:14:25 +02:00
|
|
|
|
2014-12-04 16:15:09 +01:00
|
|
|
__comment__ = {"java": "//", "c": "//", "cc": "//", "cpp": "//", "h": "//", "hh": "//", "hpp": "//", "hs": "--", "pl": "#", "php": "//",
|
2014-11-02 22:35:54 +01:00
|
|
|
"py": "#", "glsl": "//", "rb": "#", "js": "//", "scala": "//", "sql": "--", "tex": "%", "ada": "--", "ads": "--",
|
2014-11-29 07:01:24 +01:00
|
|
|
"adb": "--", "pot": "#", "po": "#" }
|
2013-06-30 01:53:08 +02:00
|
|
|
|
|
|
|
__comment_markers_must_be_at_begining__ = {"tex": True}
|
2012-05-09 12:14:25 +02:00
|
|
|
|
2013-07-02 23:03:09 +02:00
|
|
|
def __has_comment_begining__(extension, string):
|
2013-06-30 01:53:08 +02:00
|
|
|
if __comment_markers_must_be_at_begining__.get(extension, None) == True:
|
|
|
|
return string.find(__comment_begining__[extension]) == 0
|
|
|
|
elif __comment_begining__.get(extension, None) != None and string.find(__comment_end__[extension], 2) == -1:
|
2012-05-16 01:49:23 +02:00
|
|
|
return string.find(__comment_begining__[extension]) != -1
|
2013-06-30 01:53:08 +02:00
|
|
|
|
|
|
|
return False
|
2012-05-09 12:14:25 +02:00
|
|
|
|
2013-07-02 23:03:09 +02:00
|
|
|
def __has_comment_end__(extension, string):
|
2013-06-30 01:53:08 +02:00
|
|
|
if __comment_markers_must_be_at_begining__.get(extension, None) == True:
|
|
|
|
return string.find(__comment_end__[extension]) == 0
|
|
|
|
elif __comment_end__.get(extension, None) != None:
|
2012-05-16 01:49:23 +02:00
|
|
|
return string.find(__comment_end__[extension]) != -1
|
2013-06-30 01:53:08 +02:00
|
|
|
|
|
|
|
return False
|
2013-07-02 23:03:09 +02:00
|
|
|
|
|
|
|
def is_comment(extension, string):
|
|
|
|
if __comment_begining__.get(extension, None) != None and string.strip().startswith(__comment_begining__[extension]):
|
|
|
|
return True
|
|
|
|
if __comment_end__.get(extension, None) != None and string.strip().endswith(__comment_end__[extension]):
|
|
|
|
return True
|
|
|
|
if __comment__.get(extension, None) != None and string.strip().startswith(__comment__[extension]):
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def handle_comment_block(is_inside_comment, extension, content):
|
|
|
|
comments = 0
|
|
|
|
|
|
|
|
if is_comment(extension, content):
|
|
|
|
comments += 1
|
|
|
|
if is_inside_comment:
|
|
|
|
if __has_comment_end__(extension, content):
|
|
|
|
is_inside_comment = False
|
|
|
|
else:
|
|
|
|
comments += 1
|
|
|
|
elif __has_comment_begining__(extension, content) and not __has_comment_end__(extension, content):
|
|
|
|
is_inside_comment = True
|
|
|
|
|
|
|
|
return (comments, is_inside_comment)
|