c-md/srcs/transpile/core.c.md
2026-01-12 16:22:13 +01:00

1.7 KiB

core.c

Core funtion of the transpiler. Handles reading lines and processing them according to the current state.


Includes

#include <stdlib.h>

#include "transpile.h"
#include "internal/transpile_internal.h"
#include "utils.h"

Forward Declarations

static int8_t	process_line(t_state *s, char *line);

Function Descriptions

transpile

Transpiles input from a file to an output file based on the specified extension and mapping.

Parameters

  • in: Pointer to the input file.
  • out: Pointer to the output file.
  • ext: Pointer to the file extension string.
  • map: Pointer to the mapping structure.

Return Value

Return 0 on success, or a non-zero error code on failure.

Implementation

int8_t
transpile(FILE *in, FILE *out, const char *ext, t_map *map)
{
	t_state	s;
	char	*line;
	int8_t	ret;

	state_init(&s, in, out, ext, map);
	ret = 0;
	line = read_line(in);
	while (NULL != line && 0 == ret)
	{
		ret = process_line(&s, line);
		free(line);
		line = read_line(in);
	}
	free(line);
	return (ret);
}

process_line

Processes a single line of input based on the current state.

Parameters

  • s: Pointer to the current state structure.
  • line: Pointer to the line of text to process.

Return Value

Return 0 on success, or a non-zero error code on failure.

Implementation

static int8_t
process_line(t_state *s, char *line)
{
	s->src_line++;
	if (0 == s->in_block && starts_with(line, "```"))
		return (handle_fence_open(s, line));
	if (1 == s->in_block && starts_with(line, "```"))
		return (handle_fence_close(s));
	if (1 == s->in_block)
		return (handle_code_line(s, line));
	return (0);
}