Added support for costume languages. Fix #104

This commit is contained in:
Christoph Koschel 2023-09-30 19:39:39 +02:00
parent b27611eed1
commit c70aafa22a
2 changed files with 83 additions and 17 deletions

View File

@ -79,4 +79,12 @@ inputs:
ESCAPE:
description: 'Escape all special Markdown characters'
required: false
default: true
default: true
LANGUAGES:
description: 'A collection of comma-delimited URLs or local paths starting from the current working directory of the action for costume languages'
required: false
default: ''
NO_STANDARD:
description: 'Exclude loading the default ''syntax.json'' and ''language.yml'' files from the repository'
required: false
default: false

90
main.py
View File

@ -297,7 +297,6 @@ class TodoParser(object):
def __init__(self):
# Determine if the Issues should be escaped.
self.should_escape = os.getenv('INPUT_ESCAPE', 'true') == 'true'
# Load any custom identifiers, otherwise use the default.
custom_identifiers = os.getenv('INPUT_IDENTIFIERS')
self.identifiers = ['TODO']
@ -315,23 +314,82 @@ class TodoParser(object):
self.languages_dict = None
# Load the languages data for ascertaining file types.
languages_url = 'https://raw.githubusercontent.com/github/linguist/master/lib/linguist/languages.yml'
languages_request = requests.get(url=languages_url)
if languages_request.status_code == 200:
languages_data = languages_request.text
yaml = YAML(typ='safe')
self.languages_dict = yaml.load(languages_data)
else:
raise Exception('Cannot retrieve languages data. Operation will abort.')
# Check if the standard collections should be loaded
if os.getenv('INPUT_NO_STANDARD', 'false') != 'true':
# Load the languages data for ascertaining file types.
languages_url = 'https://raw.githubusercontent.com/github/linguist/master/lib/linguist/languages.yml'
languages_request = requests.get(url=languages_url)
if languages_request.status_code == 200:
languages_data = languages_request.text
yaml = YAML(typ='safe')
self.languages_dict = yaml.load(languages_data)
else:
raise Exception('Cannot retrieve languages data. Operation will abort.')
# Load the comment syntax data for identifying comments.
syntax_url = 'https://raw.githubusercontent.com/alstr/todo-to-issue-action/master/syntax.json'
syntax_request = requests.get(url=syntax_url)
if syntax_request.status_code == 200:
self.syntax_dict = syntax_request.json()
# Load the comment syntax data for identifying comments.
syntax_url = 'https://raw.githubusercontent.com/alstr/todo-to-issue-action/master/syntax.json'
syntax_request = requests.get(url=syntax_url)
if syntax_request.status_code == 200:
self.syntax_dict = syntax_request.json()
else:
raise Exception('Cannot retrieve syntax data. Operation will abort.')
else:
raise Exception('Cannot retrieve syntax data. Operation will abort.')
self.syntax_dict = []
self.languages_dict = {}
costume_languages = os.getenv('INPUT_LANGUAGES', '')
if costume_languages != '':
# Load all costume languages
for path in costume_languages.split(','):
try:
# Decide if the path is a url or local file
if path.startswith('http'):
languages_request = requests.get(path)
if languages_request.status_code != 200:
print('Cannot retrieve costume language file. (\''+path+'\')')
continue
data = languages_request.json()
else:
path = os.path.join(os.getcwd(), path)
if not os.path.exists(path) or not os.path.isfile(path):
print('Cannot retrieve costume language file. (\''+path+'\')')
continue
f = open(path)
data = json.load(f)
# Iterate through the definitions
for lang in data:
# Add/Replace the language definition
self.languages_dict[lang['language']] = {}
self.languages_dict[lang['language']]['type'] = ''
self.languages_dict[lang['language']]['color'] = ''
self.languages_dict[lang['language']]['extensions'] = lang['extensions']
self.languages_dict[lang['language']]['source'] = ''
self.languages_dict[lang['language']]['ace_mode'] = ''
self.languages_dict[lang['language']]['language_id'] = 0
# Check if a syntax with the language name already exists
counter = 0
exists = False
for syntax in self.syntax_dict:
if syntax['language'] == lang['language']:
exists = True
break
counter = counter + 1
if exists:
# When the syntax exists it will be popped out of the list
self.syntax_dict.pop(counter)
# And be replaced with the new syntax definition
self.syntax_dict.append({
'language': lang['language'],
'markers': lang['markers']
})
except:
print('An error occurred in the custom language file (\''+path+'\')')
print('Please check the file, or if it represents undefined behavior, create an issue at \'https://github.com/alstr/todo-to-issue-action/issues\'')
# noinspection PyTypeChecker
def parse(self, diff_file):