plugin writers

This commit is contained in:
randogoth 2023-06-09 23:09:11 +03:00
parent 34f9c0b6a9
commit 710dc8ea47
8 changed files with 86 additions and 15 deletions

View file

@ -452,20 +452,40 @@ class OrvaniTag:
for context_key in context.keys():
self.__updateindex(filename, context, context_key)
def __collect_contexts(self, context: dict):
elements = []
context_keys = list(CONTEXTS.keys())
for context_key in context_keys:
if not context_key in context:
context[context_key] = None
if context[context_key]:
if CONTEXTS[context_key]:
prefix = CONTEXTS[context_key]
else:
prefix = ''
if not type(context[context_key]) == str:
elements.append(' '.join([prefix + tag for tag in context[context_key]]))
else:
elements.append(context[context_key])
else:
elements.append(None)
return elements
def export(self, filename: str, method: str):
obj = {
'file' : Path(),
'config' : self.CONFIG,
'schema' : self.CTX,
'context' : {},
'collect' : self.__collect_contexts
}
o = SimpleNamespace(**obj)
for plugin_name in self.PLUGINS:
if 'plugins' in self.CONFIG:
if plugin_name not in self.CONFIG['plugins']:
continue
if plugin_name == method:
plugin = import_module('plugins.' + plugin_name)
plugin = import_module('plugins.' + plugin_name)
if plugin.NAME == method:
suffixes = plugin.TYPES
if plugin_name in self.CONFIG:
if 'types' in self.CONFIG[plugin_name]:
@ -475,8 +495,8 @@ class OrvaniTag:
self.__makeindex(o.file.parent, recursive=False)
items = self.INDEX['items']
for found in items.keys():
if './' + o.file.name == found:
if str(o.file).replace('./', '') == found.replace('./', ''):
o.context = items[found]
if hasattr(plugin, 'writer'):
plugin.writer(o)

View file

@ -1,12 +1,35 @@
"""
#// +orvani @python #example It even works in Python code
"""
import re
from pathlib import Path
NAME = 'hash//'
TYPES = ['.md', '.txt', '.py']
REGEX = r'\s*([@+]\w+|#\w+(?<!#//))'
PREFIX = '#// '
MLC_C = ['/*', '*/']
MLC_Python = ['"""', '"""']
MLC_Perl = ['=begin', '=end']
MAP = {
'.c' : MLC_C,
'.cgi' : MLC_Perl,
'.pl' : MLC_Perl,
'.class' : MLC_C,
'.cpp' : MLC_C,
'.cs' : MLC_C,
'.go' : MLC_C,
'.h' : MLC_C,
'.hs' : ['{-', '-}'],
'.html' : ['<!--', '-->'],
'.java' : MLC_C,
'.js' : MLC_C,
'.kt' : MLC_C,
'.md' : ['---', '---'],
'.php' : MLC_C,
'.py' : MLC_Python,
'.rb' : MLC_Perl,
'.rs' : MLC_C,
'.ts' : MLC_C,
'.txt' : ['---', '---'],
}
TYPES = MAP.keys()
def reader(o):
with o.file.open() as data:
@ -18,4 +41,22 @@ def reader(o):
note = re.sub(REGEX, '', line.strip()[3:]).lstrip()
argv = ' '.join(tags).split(' ') + [note]
context = o.context(argv)
return context
return context
def writer(o):
string = PREFIX + ' '.join([x for x in o.collect(o.context) if x != None]) + '\n'
with o.file.open() as data:
content = data.readlines()
for num, line in enumerate(content):
if line.strip().startswith("#// ") or num > 3:
break
if not num > 3 and line.strip().startswith("#// "):
content[num] = string
with o.file.open('w') as file:
file.writelines(content)
else:
frontmatter = MAP[o.file.suffix][0] + '\n' + string + MAP[o.file.suffix][1] + '\n'
with o.file.open('r+') as file:
content = file.read()
file.seek(0, 0)
file.write(frontmatter + content)

View file

@ -30,8 +30,15 @@ def reader(o):
context = o.context(argv)
if not universal in context:
context[universal] = []
else:
context[universal] = [context[universal]]
if id3map.keys():
for key in id3map.keys():
if key in context.keys():
context[key] += id3map[key]
return context
return context
def writer(o):
string = ' '.join([x for x in o.collect(o.context) if x != None])
with taglib.File(o.file, save_on_exit=True) as song:
song.tags['COMMENT'] = [string]

View file

@ -15,4 +15,10 @@ def reader(o):
argv = ' '.join(tags).split(' ') + [note]
argv = [x for x in argv if x != '']
context = o.context(argv)
return context
return context
def writer(o):
string = ' '.join([x for x in o.collect(o.context) if x != None])
pdf = PdfReader(o.file)
pdf.Info.Keywords = string
PdfWriter(o.file, trailer=pdf).write()