Skip to content

Commit

Permalink
Merge pull request #25 from Aman-Codes/scss_branch
Browse files Browse the repository at this point in the history
feat(scss): added support for scss

Reviewed-by: [email protected]
Tested-by: [email protected]
  • Loading branch information
GMishx authored Dec 18, 2020
2 parents 0da4db0 + 2973f79 commit 5f86a9b
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ The Languages we support till now:
- Ruby
- Rust
- Scala
- Scss
- Shell
- Swift
- TypeScript
Expand Down
4 changes: 2 additions & 2 deletions nirjas/binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def readSingleLine(file, regex, sign):
output = ''.join(output)

if output:
output = output[1:]
output = output[len(sign):]
content.append([lineNumber, output.strip()])

line = line.replace(" ","")
Expand Down Expand Up @@ -161,7 +161,7 @@ def doubleNotTripleSlash(self,file):
sign: //
'''
self.sign = '//'
self.pattern_doubleNotTripleSlash = r'''([^\/]\/\/[^\/]\s*[\w #\.()@+-_*\d]*)'''
self.pattern_doubleNotTripleSlash = r'''((?<!\/)\/\/(?!\/)\s*[\w #\.()@+-_*\d]*)'''
return readSingleLine(file, self.pattern_doubleNotTripleSlash, self.sign)

def singleQuotes(self,file):
Expand Down
2 changes: 1 addition & 1 deletion nirjas/languages/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__all__ = ["c", "c_sharp", "cpp", "css", "dart", "go", "haskell", "html", "java", "javascript", "kotlin", "matlab", "perl", "php", "python", "r", "ruby", "rust", "scala", "shell", "swift", "text", "typescript"]
__all__ = ["c", "c_sharp", "cpp", "css", "dart", "go", "haskell", "html", "java", "javascript", "kotlin", "matlab", "perl", "php", "python", "r", "ruby", "rust", "scala", "scss", "shell", "swift", "text", "typescript"]
106 changes: 106 additions & 0 deletions nirjas/languages/scss.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Copyright (C) 2020 Aman Dwivedi ([email protected])
SPDX-License-Identifier: LGPL-2.1
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''

from nirjas.binder import *

def scssExtractor(file):
result = CommentSyntax()
result1 = result.doubleNotTripleSlash(file)
result2 = result.slashStar(file)
result3 = result.tripleSlash(file)
result4 = contSingleLines(result1)
result5 = contSingleLines(result3)
file = file.split("/")
output = {
"metadata": [{
"filename": file[-1],
"lang": "Scss",
"total_lines": result1[1],
"total_lines_of_comments": result1[3]+result2[3]+result3[3],
"blank_lines": result1[2],
"sloc": result1[1]-(result1[3]+result2[3]+result3[3]+result1[2])
}],
"single_line_comment": [],
"cont_single_line_comment": [],
"multi_line_comment": []
}

if result4:
result1 = result4[0]

if result5:
result3 = result5[0]

if result1:
for i in result1[0]:
output['single_line_comment'].append({"line_number" :i[0],"comment": i[1]})

if result3:
for i in result3[0]:
output['single_line_comment'].append({"line_number" :i[0],"comment": i[1]})

if result4:
for idx,i in enumerate(result4[1]):
output['cont_single_line_comment'].append({"start_line": result4[1][idx], "end_line": result4[2][idx], "comment": result4[3][idx]})

if result5:
for idx,i in enumerate(result5[1]):
output['cont_single_line_comment'].append({"start_line": result5[1][idx], "end_line": result5[2][idx], "comment": result5[3][idx]})

if result2:
try:
for idx,i in enumerate(result2[0]):
output['multi_line_comment'].append({"start_line": result2[0][idx], "end_line": result2[1][idx], "comment": result2[2][idx]})
except:
pass

return output

def scssSource(file, newFile: str):
closingCount = 0
copy = True
with open(newFile, 'w+') as f1:
with open(file) as f:
for lineNumber, line in enumerate(f, start=1):
if line.strip() == '/*':
closingCount+=1
copy = False
if closingCount%2 == 0:
copy = True

if line.strip() == '*/':
closingCount+=1
copy = False
if closingCount%2 == 0:
copy = True

if copy:
if line.strip() != '/*' and line.strip() != '*/':
Templine = line.replace(" ","")
if Templine[0:3] != "///": # Syntax for single line documentation comment
f1.write(line)
elif Templine[0:2] != "//": # Syntax for single line comment
f1.write(line)

f.close()
f1.close()
return newFile
4 changes: 2 additions & 2 deletions nirjas/languages/tests/test_dart.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DartTest(unittest.TestCase):
testfile = os.path.join(os.path.abspath(os.path.dirname(__file__)), "TestFiles/textcomment.dart")

def test_output(self):
regex1 = r'''([^\/]\/\/[^\/]\s*[\w #\.()@+-_*\d]*)'''
regex1 = r'''((?<!\/)\/\/(?!\/)\s*[\w #\.()@+-_*\d]*)'''
regex2 = r'''(\/\/\/\s*[\w #\.()@+-_*\d]*)'''
self.syntax_start = "/*"
self.syntax_end ='*/'
Expand All @@ -46,7 +46,7 @@ def test_output(self):


def test_outputFormat(self):
regex1 = r'''([^\/]\/\/[^\/]\s*[\w #\.()@+-_*\d]*)'''
regex1 = r'''((?<!\/)\/\/(?!\/)\s*[\w #\.()@+-_*\d]*)'''
regex2 = r'''(\/\/\/\s*[\w #\.()@+-_*\d]*)'''
self.syntax_start = "/*"
self.syntax_end ='*/'
Expand Down
111 changes: 111 additions & 0 deletions nirjas/languages/tests/test_scss.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
'''
Copyright (C) 2020 Aman Dwivedi ([email protected])
SPDX-License-Identifier: LGPL-2.1
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''

import unittest
import os
from nirjas.languages import scss
from nirjas.binder import readSingleLine,readMultiLineDiff

class ScssTest(unittest.TestCase):
testfile = os.path.join(os.path.abspath(os.path.dirname(__file__)), "TestFiles/textcomment.scss")

def test_output(self):
regex1 = r'''((?<!\/)\/\/(?!\/)\s*[\w #\.()@+-_*\d]*)'''
regex2 = r'''(\/\/\/\s*[\w #\.()@+-_*\d]*)'''
self.syntax_start = "/*"
self.syntax_end ='*/'
sign1 = '//'
sign2 = '///'
comment_single_doubleSlash = scss.readSingleLine(self.testfile,regex1,sign1)
comment_single_tripleSlash = scss.readSingleLine(self.testfile,regex2,sign2)
comment_multiline = scss.readMultiLineDiff(self.testfile,self.syntax_start,self.syntax_end)
comment_contSingleline1 = scss.contSingleLines(comment_single_doubleSlash)
comment_contSingleline2 = scss.contSingleLines(comment_single_tripleSlash)
self.assertTrue(comment_single_doubleSlash)
self.assertTrue(comment_single_tripleSlash)
self.assertTrue(comment_multiline)
self.assertTrue(comment_contSingleline1)
self.assertTrue(comment_contSingleline2)


def test_outputFormat(self):
regex1 = r'''((?<!\/)\/\/(?!\/)\s*[\w #\.()@+-_*\d]*)'''
regex2 = r'''(\/\/\/\s*[\w #\.()@+-_*\d]*)'''
self.syntax_start = "/*"
self.syntax_end ='*/'
sign1 = '//'
sign2 = '///'
expected = scss.scssExtractor(self.testfile)
comment_single_doubleSlash = scss.readSingleLine(self.testfile,regex1,sign1)
comment_single_tripleSlash = scss.readSingleLine(self.testfile,regex2,sign2)
comment_multiline = scss.readMultiLineDiff(self.testfile,self.syntax_start,self.syntax_end)
comment_contSingleline1 = scss.contSingleLines(comment_single_doubleSlash)
comment_contSingleline2 = scss.contSingleLines(comment_single_tripleSlash)
file = self.testfile.split("/")
output = {
"metadata": [{
"filename": file[-1],
"lang": "Scss",
"total_lines": comment_single_doubleSlash[1],
"total_lines_of_comments": comment_single_doubleSlash[3]+comment_single_tripleSlash[3]+comment_multiline[3],
"blank_lines": comment_single_doubleSlash[2],
"sloc": comment_single_doubleSlash[1]-(comment_single_doubleSlash[3]+comment_single_tripleSlash[3]+comment_multiline[3]+comment_single_doubleSlash[2])
}],
"single_line_comment": [],
"cont_single_line_comment": [],
"multi_line_comment": []
}

if comment_contSingleline1:
comment_single_doubleSlash = comment_contSingleline1[0]

if comment_contSingleline2:
comment_single_tripleSlash = comment_contSingleline2[0]

if comment_single_doubleSlash:
for i in comment_single_doubleSlash[0]:
output['single_line_comment'].append({"line_number" :i[0],"comment": i[1]})

if comment_single_tripleSlash:
for i in comment_single_tripleSlash[0]:
output['single_line_comment'].append({"line_number" :i[0],"comment": i[1]})

if comment_contSingleline1:
for idx,i in enumerate(comment_contSingleline1[1]):
output['cont_single_line_comment'].append({"start_line": comment_contSingleline1[1][idx], "end_line": comment_contSingleline1[2][idx], "comment": comment_contSingleline1[3][idx]})

if comment_contSingleline2:
for idx,i in enumerate(comment_contSingleline2[1]):
output['cont_single_line_comment'].append({"start_line": comment_contSingleline2[1][idx], "end_line": comment_contSingleline2[2][idx], "comment": comment_contSingleline2[3][idx]})

if comment_multiline:
try:
for idx,i in enumerate(comment_multiline[0]):
output['multi_line_comment'].append({"start_line": comment_multiline[0][idx], "end_line": comment_multiline[1][idx], "comment": comment_multiline[2][idx]})
except:
pass

self.assertEqual(output,expected)

def test_Source(self):
name = "source.txt"
newfile = scss.scssSource(self.testfile,name)

self.assertTrue(newfile)
1 change: 1 addition & 0 deletions nirjas/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def langIdentifier(file):
'.hxx': 'cpp',
'.cc': 'cpp',
'.css': 'css',
'.scss': 'scss',
'.dart': 'dart',
'.go': 'go',
'.hs': 'haskell',
Expand Down
3 changes: 2 additions & 1 deletion testScript.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def download_files(cwd):
"https://raw.githubusercontent.com/apache/thrift/f86845e8ed622e7e3b7c87f00f16729ee6cc524d/lib/ts/test/phantom-client.ts",
"https://raw.githubusercontent.com/airbnb/react-with-styles/2532394bb866aaade4dc750ee94c0ff213d9b6de/src/withStyles.jsx",
"https://raw.githubusercontent.com/flutter/plugins/e61e9d45bcaadc3e409d529d30735cb4db75c5c5/packages/android_alarm_manager/lib/android_alarm_manager.dart",
"https://raw.githubusercontent.com/microsoft/TypeScript/c33a14d66d0a452673ce77256e178bf84e875d2b/tests/cases/user/formik/index.tsx"
"https://raw.githubusercontent.com/microsoft/TypeScript/c33a14d66d0a452673ce77256e178bf84e875d2b/tests/cases/user/formik/index.tsx",
"https://raw.githubusercontent.com/twbs/bootstrap/9488978fb55286ba83e8193a871d1ff9815045b9/scss/_reboot.scss"
]

directory = os.path.join(cwd, "nirjas/languages/tests/TestFiles")
Expand Down

0 comments on commit 5f86a9b

Please sign in to comment.