// Written by retoor@molodetz.nl

// This source code extracts URLs from an input string and replaces them inline
// with their respective content in Markdown style.

// Imports used: regex.h for regular expression operations, http.h and url.h for
// HTTP and URL-related utility functions.

// MIT License

#include "http.h"
#include "url.h"
#include <regex.h>
#include <stdlib.h>
#include <string.h>

void extract_urls(const char *input, char **urls, int *url_count) {
  const char *pattern = "https?://[^ ]+";
  regex_t regex;
  regcomp(&regex, pattern, REG_EXTENDED);
  regmatch_t match;
  const char *cursor = input;

  *url_count = 0;
  while (!regexec(&regex, cursor, 1, &match, 0)) {
    int length = match.rm_eo - match.rm_so;
    urls[*url_count] = strndup(cursor + match.rm_so, length);
    cursor += match.rm_eo;
    (*url_count)++;
  }
  regfree(&regex);
}

void inplace_urls_markdown_style(char **input, char **urls, char **contents,
                                 int url_count) {
  for (int i = 0; i < url_count; i++) {
    char *found = strstr(*input, urls[i]);
    if (found) {
      char *new_text = (char *)malloc(strlen(*input) + strlen(contents[i]) -
                                      strlen(urls[i]) + 1);
      strncpy(new_text, *input, found - *input);
      new_text[found - *input] = '\0';
      strcat(new_text, contents[i]);
      strcat(new_text, found + strlen(urls[i]));

      free(*input);
      *input = new_text;
    }
  }
}