# Path: porcupine/plugins/urls.py
# def find_urls(text: tkinter.Text, start: str, end: str) -> Iterable[tuple[str, str]]:
#     match_ends_and_search_begins = start
#     while True:
#         match_start = text.search(
#             r"\mhttps?://[a-z0-9:]", match_ends_and_search_begins, end, nocase=True, regexp=True
#         )
#         if not match_start:  # empty string means not found
#             break
# 
#         url = text.get(match_start, f"{match_start} lineend")
#         before_url = (
#             None if text.index(match_start) == "1.0" else text.get(f"{match_start} - 1 char")
#         )
# 
#         # urls end on space or quote
#         url = url.split(" ")[0]
#         url = url.split("'")[0]
#         url = url.split('"')[0]
#         url = url.split("`")[0]
# 
#         open2close = {"(": ")", "{": "}", "<": ">"}
#         close2open = {")": "(", "}": "{", ">": "<"}
# 
#         if before_url in open2close and open2close[before_url] in url:
#             # url is parenthesized
#             url = url.split(open2close[before_url])[0]
#         if url[-1] in close2open and close2open[url[-1]] not in url:
#             # url isn't like "Bla(bla)" but ends with ")" or similar, assume that's not part of url
#             url = url[:-1]
# 
#         # urls in middle of text: URL, and URL.
#         url = url.rstrip(".,")
# 
#         match_ends_and_search_begins = f"{match_start} + {len(url)} chars"
#         yield (match_start, match_ends_and_search_begins)


import tkinter
import pytest
from porcupine.plugins.urls import find_urls



simple_urls = [
    "https://github.com/Akuli/porcupine/",
    "http://example.com/",
    "http://example.com/comma,stuff",
    "http://127.0.0.1:12345/foo.html",
]
parenthesized_urls = [
    "https://en.wikipedia.org/wiki/Whitespace_(programming_language)",
    "https://example.com/foo(bar)baz",
]

simple_text = """\
          URL
      bla URL
      bla URL bla
          URL bla bla
      Bla URL.
 Bla bla  URL.
 Bla bla  URL, foo and bar.
     bla "URL" bla
     bla 'URL' bla

# Path: porcupine/plugins/urls.py
# def find_urls(text: tkinter.Text, start: str, end: str) -> Iterable[tuple[str, str]]:
#     match_ends_and_search_begins = start
#     while True:
#         match_start = text.search(
#             r"\mhttps?://[a-z0-9:]", match_ends_and_search_begins, end, nocase=True, regexp=True
#         )
#         if not match_start:  # empty string means not found
#             break
# 
#         url = text.get(match_start, f"{match_start} lineend")
#         before_url = (
#             None if text.index(match_start) == "1.0" else text.get(f"{match_start} - 1 char")
#         )
# 
#         # urls end on space or quote
#         url = url.split(" ")[0]
#         url = url.split("'")[0]
#         url = url.split('"')[0]
#         url = url.split("`")[0]
# 
#         open2close = {"(": ")", "{": "}", "<": ">"}
#         close2open = {")": "(", "}": "{", ">": "<"}
# 
#         if before_url in open2close and open2close[before_url] in url:
#             # url is parenthesized
#             url = url.split(open2close[before_url])[0]
#         if url[-1] in close2open and close2open[url[-1]] not in url:
#             # url isn't like "Bla(bla)" but ends with ")" or similar, assume that's not part of url
#             url = url[:-1]
# 
#         # urls in middle of text: URL, and URL.
#         url = url.rstrip(".,")
# 
#         match_ends_and_search_begins = f"{match_start} + {len(url)} chars"
#         yield (match_start, match_ends_and_search_begins)


# Path: tests/test_urls_plugin.py
import tkinter
import pytest
from porcupine.plugins.urls import find_urls




simple_urls = [
    "https://github.com/Akuli/porcupine/",
    "http://example.com/",
    "http://example.com/comma,stuff",
    "http://127.0.0.1:12345/foo.html",
]
parenthesized_urls = [
    "https://en.wikipedia.org/wiki/Whitespace_(programming_language)",
    "https://example.com/foo(bar)baz",
]

simple_text = """\
          URL
      bla URL
      bla URL bla
          URL bla bla
      Bla URL.
 Bla bla  URL.
 Bla bla  URL, foo and bar.
     bla "URL" bla
     bla 'URL' bla

         `URL`