Add basic aisl tool implementation

This commit is contained in:
2019-07-14 20:56:28 +02:00
parent ce7eda1820
commit 043d414df4
9 changed files with 282 additions and 605 deletions
+84
View File
@@ -0,0 +1,84 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file compose.c
* @author Ilja Kartašov <ik@lowenware.com>
* @brief
*
* @see https://lowenware.com/
*/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <components/html.h>
#include "option.h"
#include "compose.h"
static int
compose_help(void)
{
printf("NAME\n");
printf("\taisl-compose - Compose plain text resource\n\n");
printf("SYNOPSIS\n");
printf("\taisl compose [--format=auto|html|js|css] <source> <target>\n\n");
printf("DESCRIPTION\n");
printf("\tComposes a plain text resource to its production version.");
printf("OPTIONS\n");
printf("\t--format=auto|html|js|css\n");
printf("\t Manually set format for the composer. Usefull when file "
"extension does not allow to determine type automatically.\n\n");
return 0;
}
int
compose_execute(int argc, char **argv)
{
int result;
struct cstuff_list list;
struct ax_html_options opts = {0};
if (g_options & OPTION_FLAG_HELP) {
return compose_help();
}
result = -1;
if (!(argc == 1 || argc == 2)) {
return result;
}
g_source = argv[0];
if (argc == 2) {
g_target = argv[1];
}
opts.offset = "\t";
opts.path = g_target ? g_target : ".";
opts.name = "html";
opts.minimize = true;
if (!cstuff_list_init(&list, 128)) {
if (ax_html_import(&list, g_source) == AISL_SUCCESS) {
if (ax_html_export(&list, &opts) == AISL_SUCCESS) {
result = 0;
} else {
fprintf(stderr, "Export failed\n");
}
} else {
fprintf(stderr, "Import failed: %s\n", strerror(errno));
}
cstuff_list_release(&list, (CStuffListFree)ax_html_free);
}
return result;
}
+23
View File
@@ -0,0 +1,23 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file compose.h
* @author Ilja Kartašov <ik@lowenware.com>
* @brief
*
* @see https://lowenware.com/
*/
#ifndef COMPOSE_H_006245E9_DAFB_4160_AB48_2268FA6E0769
#define COMPOSE_H_006245E9_DAFB_4160_AB48_2268FA6E0769
int
compose_execute(int argc, char **argv);
#endif /* !COMPOSE_H */
+23
View File
@@ -0,0 +1,23 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file globals.h
* @author Ilja Kartašov <ik@lowenware.com>
* @brief
*
* @see https://lowenware.com/
*/
#ifndef GLOBALS_H_ECAB54AF_855F_408C_9CC1_65B994859848
#define GLOBALS_H_ECAB54AF_855F_408C_9CC1_65B994859848
#include <stdint.h>
extern uint32_t g_options;
#endif /* !GLOBALS_H */
+40
View File
@@ -0,0 +1,40 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file main.c
* @author Ilja Kartašov <ik@lowenware.com>
* @brief
*
* @see https://lowenware.com/
*/
#include <stdio.h>
#include "option.h"
#include "compose.h"
int
main(int argc, char **argv)
{
int result;
if ((result = option_read_args(argc, argv)) != -1) {
switch (g_options & 0xFF) {
case OPTION_COMMAND_COMPOSE:
if (!compose_execute(argc - result, &argv[result]))
return 0;
break;
default:
result = -1;
break;
}
}
printf("For usage guideance run `%s --help`\n", argv[0]);
return result;
}
+72
View File
@@ -0,0 +1,72 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file option.c
* @author Ilja Kartašov <ik@lowenware.com>
* @brief
*
* @see https://lowenware.com/
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "option.h"
uint32_t g_options = 0;
const char *g_format = NULL;
const char *g_source = NULL;
const char *g_target = NULL;
static void
option_set_command(int command)
{
if (!(g_options & 0xFF)) {
g_options |= command;
}
}
int
option_read_args(int argc, char **argv)
{
int i;
char c = 0;
for (i = 1; i < argc; i++) {
switch (c) {
case 'f':
g_format = argv[i];
break;
default:
if (strcmp(argv[i], "-f") == 0) {
c = 'f';
} else if (!strncmp(argv[i], "--format=", 9)) {
g_format = &argv[i][9];
} else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
g_options |= OPTION_FLAG_HELP;
} else if (!strcmp(argv[i], "compose")) {
option_set_command(OPTION_COMMAND_COMPOSE);
} else {
return i;
}
continue;
}
c = 0;
}
if (c) {
fprintf(stderr, "No value for -%c argument", c);
return -1;
}
return i - 1;
}
+39
View File
@@ -0,0 +1,39 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file option.h
* @author Ilja Kartašov <ik@lowenware.com>
* @brief
*
* @see https://lowenware.com/
*/
#ifndef OPTION_H_FC8ABD38_1EF9_4DD2_A94A_3C4F53E0A437
#define OPTION_H_FC8ABD38_1EF9_4DD2_A94A_3C4F53E0A437
#include <stdint.h>
#define OPTION_FLAG_HELP (1 << 16)
enum {
OPTION_COMMAND_UNKNOWN
, OPTION_COMMAND_COMPOSE
};
extern uint32_t g_options;
extern const char *g_format;
extern const char *g_source;
extern const char *g_target;
int
option_read_args(int argc, char **argv);
#endif /* !OPTION_H */