Skip to content

Commit

Permalink
try to fix formatting workflow and format files locally
Browse files Browse the repository at this point in the history
  • Loading branch information
CallMeCrazYN committed Jul 1, 2023
1 parent 282d9d1 commit 8f8cd5d
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 73 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/black.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: Lint
name: Black Formatter

on: [push, pull_request]
branches:
- main

jobs:
lint:
Expand All @@ -9,5 +11,5 @@ jobs:
- uses: actions/checkout@v3
- uses: psf/black@stable
with:
options: "--check --verbose"
options: "--verbose"
src: "./src"
39 changes: 18 additions & 21 deletions src/block_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
from typing import TextIO
from utility import text_file_peek_line

tags_dict = {
'startTag': '{card}',
'sepTag': '----'
}
tags_dict = {"startTag": "{card}", "sepTag": "----"}

valid_tag_list = tags_dict.values()

Expand All @@ -19,17 +16,17 @@ def get_cards(file_path: str) -> list:

cardlist = []
if os.path.exists(file_path):
f = open(file_path, 'r', encoding='utf-8')
f = open(file_path, "r", encoding="utf-8")

# until end of file is reached try to fetch cards
while text_file_peek_line(f) != '':
while text_file_peek_line(f) != "":
# get a card from the current file position
next_card = get_next_card(f)
if next_card is not None:
# add card to the list
cardlist.append(next_card)
else:
print('File could not be found:' + file_path)
print("File could not be found:" + file_path)

return cardlist

Expand All @@ -43,16 +40,16 @@ def get_next_card(file: TextIO) -> list:
card = [[], [], []]

# find start, get tag from tuple for later usage
card[0] = get_next_Tag(file, tags_dict['startTag'])[0]
card[0] = get_next_Tag(file, tags_dict["startTag"])[0]

# attempt to find card separator
tag, content = get_next_Tags(file, valid_tag_list)

print('Content:' + content)
print("Content:" + content)

print('Tag:' + str(tag))
print("Tag:" + str(tag))

if tag == tags_dict['sepTag']:
if tag == tags_dict["sepTag"]:
card[1] = content
else:
# card is not valid
Expand All @@ -65,7 +62,7 @@ def get_next_card(file: TextIO) -> list:

tag, content = get_next_Tags(file, valid_tag_list)

if tag == tags_dict['sepTag'] or tag == tags_dict['startTag']:
if tag == tags_dict["sepTag"] or tag == tags_dict["startTag"]:
card[2] = content
else:
# card is not valid
Expand All @@ -79,7 +76,7 @@ def get_next_card(file: TextIO) -> list:

def get_next_Tag(file: TextIO, searchTag: str) -> Tuple[str, str]:
"""
allows the passing of just a string which will
allows the passing of just a string which will
then be put in list, so it can be passed to get_next_Tags
"""
tag_as_list = [searchTag]
Expand All @@ -90,22 +87,22 @@ def get_next_Tags(file: TextIO, validTags: list) -> Tuple[str, str]:
"""
gets the next Tag from the list
"""
content = ''
found_tag = ''
currline = 'something thats not empty'
content = ""
found_tag = ""
currline = "something thats not empty"
# read till EOF
while not currline == '':
while not currline == "":
currline = text_file_peek_line(file)
print(validTags)
print(currline)
for tag in validTags:
print('CurrTag:' + str(tag))
print("CurrTag:" + str(tag))
if currline.startswith(tag):
found_tag = tag
break

file.readline()
if found_tag != '':
if found_tag != "":
break

content += currline
Expand All @@ -117,7 +114,7 @@ def reset_position_by_tag(tag: str, file: TextIO):
reset the file pointer to the position before the tag was read
"""
# dont reset if empty line was found
if (tag == ''):
if tag == "":
pass
else:
file.seek(file.tell() - (len(tag)+1), 0)
file.seek(file.tell() - (len(tag) + 1), 0)
37 changes: 24 additions & 13 deletions src/file_loader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from tags import start_tag


def load_multiple_files(path: str, deck_tag: str) -> list:
"""
loads all files that have a start tag and are for the given deck.
Expand Down Expand Up @@ -34,10 +35,10 @@ def load_one_file(file_name: str, deck_tag: str) -> str:
f = open(file_name, "r")
try:
f.readline()[-1]
except IndexError:
f.seek(0)
except IndexError:
print("File not working with tags.")
return ""
f.seek(0)
check_flash = contains_tag(f, start_tag)
check_deck = contains_tag(f, deck_tag)
if check_flash is False:
Expand All @@ -46,7 +47,6 @@ def load_one_file(file_name: str, deck_tag: str) -> str:
if check_deck is False:
print("No belonging deck in file.")
return ""
f.seek(0)
return f.read()
except FileNotFoundError:
print("File not found, try again.")
Expand All @@ -57,24 +57,35 @@ def contains_tag(file: object, tag: str) -> bool:
"""
Looks at the first line of the given file and searches for the tag.
"""

foundTag = False
prevPosition = file.tell()
file.seek(0)

if tag in file.readline():
return True
foundTag = True

if tag in file.readlines()[-1]:
return True

return False
foundTag = True

# reset position in the file handle
file.seek(prevPosition)
return foundTag


# str-version of tag-checker
def contains_tag_str(file: str, tag: str) -> bool:
"""
Looks at the first line of the given file and searches for the tag.
"""


foundTag = False
prevPosition = file.tell()
file.seek(0)

if tag in file.partition("\n")[-1]:
return True

return False
foundTag = True
# reset file handle position
file.seek(prevPosition)

return foundTag
4 changes: 2 additions & 2 deletions src/learningcards.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def parse_md_cards(file_string: str) -> list:
# empty line skipped
if line == "":
continue

if line == start_tag:
continue

Expand Down Expand Up @@ -44,7 +44,7 @@ def parse_md_cards(file_string: str) -> list:

# content of simplecard
elif card_control["simple"] == True:
learningcard_list[len(learningcard_list)-1].set_back_content(line)
learningcard_list[len(learningcard_list) - 1].set_back_content(line)

# content of questioncard
elif card_control["question"] == True:
Expand Down
16 changes: 10 additions & 6 deletions src/md_to_anki.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def path_to_anki(path: str, deck_tag: str, deck_name: str):

anki_deck = genanki.Deck(id_generator(), deck_name)

anki_deck = genanki.Deck(id_generator(),deck_name)
anki_deck = genanki.Deck(id_generator(), deck_name)

for card in md_cards:
note_list = anki_note_from_list(card)
for note in note_list:
Expand Down Expand Up @@ -109,23 +109,24 @@ def anki_note_from_list(card_list: list):
return note_list


def get_media_from_path(path : str)-> list:
def get_media_from_path(path: str) -> list:
"""
Returns every media file from the path as a list.
Media is a png, jpeg, mp3, gif or mp4.
"""
os.chdir(path)
os.chdir(path)
files = os.listdir()
media_files = []
for x in files:
if x[-4:] == ".png" or x[-4:] == ".mp3" or x[-4:] == ".gif" or x[-4:] == ".mp4":
if x[-4:] == ".png" or x[-4:] == ".mp3" or x[-4:] == ".gif" or x[-4:] == ".mp4":
media_files.append(x)
elif x[-5:] == ".jpeg":
media_files.append(x)
else:
continue
return media_files


def id_generator():
"""
Creates a random id for the model- and deck-identification.
Expand All @@ -138,4 +139,7 @@ def id_generator():
# test
# md_to_anki("/Users/joinas/Documents/Uni/Software-Engineering/Markdown-Anki/Markdown-LearningCards", "#fileTest", "TagTest")

md_to_anki("/Users/joinas/Documents/Obsidian/Life","#AlgoGeo","AlgoGeoTest")
# md_to_anki("/Users/joinas/Documents/Obsidian/Life","#AlgoGeo","AlgoGeoTest")


md_to_anki("./testDir/", "#Mango", "MangoTest")
47 changes: 20 additions & 27 deletions src/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,28 @@

# dict of tags for the md syntax of the cards
tags_md = {
"start" : "# ",
"section" : "## ",
"question_card" : "{QUESTION}",
"front" : "{FRONT}",
"back" : "{BACK}",
"bold" : "**",
"italic" : "*",
"strike" : "~~",
"heading1" : "#",
"heading2" : "##",
"heading3" : "###",
"heading4" : "####",
"heading5" : "#####",
"heading6" : "######",
"ulist1" : "-",
"ulist2" : "+",
"ulist3" : "*",
"code" : "`"
"start": "# ",
"section": "## ",
"question_card": "{QUESTION}",
"front": "{FRONT}",
"back": "{BACK}",
"bold": "**",
"italic": "*",
"strike": "~~",
"heading1": "#",
"heading2": "##",
"heading3": "###",
"heading4": "####",
"heading5": "#####",
"heading6": "######",
"ulist1": "-",
"ulist2": "+",
"ulist3": "*",
"code": "`",
}

# controls the file traversal
card_control = {
"simple" : False,
"question" : False,
"back" : False
}
card_control = {"simple": False, "question": False, "back": False}

# html syntax that will replace md
tags_html = {
"strike_begin" : "<del>",
"strike_end" : "</del>"
}
tags_html = {"strike_begin": "<del>", "strike_end": "</del>"}
2 changes: 1 addition & 1 deletion src/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
cards = get_cards(str(input()))

for card in cards:
print('\n'.join(card))
print("\n".join(card))
2 changes: 1 addition & 1 deletion src/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def text_file_peek(file: TextIO, n: int) -> str:

def text_file_peek_line(file: TextIO, number_of_lines=1) -> str:
previousPosition = file.tell()
content = ''
content = ""
for i in range(number_of_lines):
content += str(file.readline())
# return to the previousPosition
Expand Down
1 change: 1 addition & 0 deletions testDir/testCard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#

0 comments on commit 8f8cd5d

Please sign in to comment.