#! /usr/bin/env python3

# TODO
# 
# check file type of file input via cli
# differentiate between:
#   plain text (.txt)
#   HTML (.htm, .html)
#   Markdown (.md)
#
# loop through all lines of the file
# search for occurrences of brackets in each line
# use round brackets only if they contain URLs of any kind (://)
# use all square brackets
#
# remove any whitespace preceding the brackets
# replace each pair of brackets with [i]
# where i is a counter that gets incremented
#
# save contents of brackets in a dictionary (?) 
# with counter variable serving as key
#
# add two new lines at end of file
# and output the dictionary sorted by keys (ascending)
# like so: [key] dictionarycontent
#

import sys
import os
import re

try:
    # Python3
    from urllib.parse import urlparse
except ImportError:
    # Python2
    from urlparse import urlparse

# check file type of file input by user via cli
# + convert extension to lower case just in case
filename = sys.argv[-1]
filename_base, extension = os.path.splitext(filename)
extension = extension.lower()

# number of first reference
counter = 1
brackets = []

if extension == ".txt":
    # read in the file
    f = open(filename, 'r')

    # iterate over all lines
    for line in f:
        # search lines using regex
        # find all round and square brackets
        # TODO: find only round brackets containing URLs
        brackets.extend(re.finditer("([ ]*[\(])([^\(\)]*)([\)])"
            "|([ ]*[\[])([^\[\]]*)([\]])", line))

    for reference in brackets:
        # all references
        # re_group = 0
        # if reference.group(re_group) is not None:
        #     print("found {} group {} {}" .format(counter, re_group, reference.group(re_group))) #debug

        # references in round brackets
        re_group = 2
        if reference.group(re_group) is not None:
            print("found {} group {} {}" .format(counter, 
                re_group, reference.group(re_group))) #debug
        # references in square brackets
        re_group = 5
        if reference.group(re_group) is not None:
            print("found {} group {} {}" .format(counter, 
                re_group, reference.group(re_group))) #debug
        counter += 1
    f.close()
elif extension == (".htm" or ".html"):
    # code for HTML files goes here
    print("html!") #debug only
elif extension == ".md":
    # code for Markdown files goes here
    print("markdown!") #debug only
else:
    print("You did not specify a valid file name.\n"
        "Only .txt, .htm/.html and .md files can be converted.")
@@ -723,17 +723,16 @@
 port re%0A
-%0A
 try:%0A   
@@ -1775,71 +1775,163 @@
     
-re_group = 2%0A        if reference.group(re_group) is not None:%0A
+brackets_round = reference.group(2)%0A        if brackets_round is not None:%0A            url = urlparse(brackets_round)%0A            if url%5B0%5D is not '':%0A    
     
@@ -1949,33 +1949,25 @@
 nt(%22found %7B%7D
- group %7B%7D
+:
  %7B%7D%22 .format
@@ -1980,69 +1980,60 @@
 er, 
-%0A                re_group, reference.group(re_group))) #debug
+brackets_round)) #debug%0A                counter += 1
 %0A   
@@ -2081,20 +2081,44 @@
     
-re_
+brackets_square = reference.
 group
- = 5
+(5)
 %0A   
@@ -2129,33 +2129,23 @@
  if 
-reference.group(re_group)
+brackets_square
  is 
@@ -2177,33 +2177,25 @@
 nt(%22found %7B%7D
- group %7B%7D
+:
  %7B%7D%22 .format
@@ -2208,60 +2208,23 @@
 er, 
-%0A                re_group, reference.group(re_group)
+brackets_square
 )) #
@@ -2221,32 +2221,36 @@
 square)) #debug%0A
+    
         counter 
@@ -2250,24 +2250,25 @@
 ounter += 1%0A
+%0A
     f.close(

check for urls in round brackets, clean up regex group code