Skip to content

Commit

Permalink
Now loading index.html file based on argument to colorscheme
Browse files Browse the repository at this point in the history
  • Loading branch information
Peterwmoss committed Nov 10, 2020
1 parent b8db96a commit 4f00fa6
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
#include <regex>
#include <string>

std::string fix_path(std::string *path) {
QString get_file(QString *path) {
std::regex e(R"((.*/)*(.*md))");
return regex_replace(*path, e, "$1");
return regex_replace(path->toStdString(), e, "$1").c_str();
}

bool file_exists(QString *path) {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

const std::string QRC_FILE(".images.rcc");

std::string fix_path(std::string *path);
QString get_file(QString *path);
bool file_exists(QString *path);
bool directory_exists(QString *path);

Expand Down
42 changes: 16 additions & 26 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
#include "mainwindow.h"

#include <QApplication>
#include <QDir>
#include <QResource>
#include <QString>
#include <cstdio>
#include <string>

int die(const char *message) {
Expand All @@ -15,36 +12,31 @@ int die(const char *message) {
exit(1);
}

bool parse_file(char *arg, QString *file, std::string path) {
bool parse_file(char *arg, QString *file) {
*file = arg;
if (file_exists(file)) {
std::string s(arg);
path = fix_path(&s);
return true;
}
return false;
}

bool parse_color(std::string color) {
std::string light = "light";
std::string dark = "dark";

if (color == light) {
color = ":/index-light.html";
bool parse_color(QString *color) {
if (*color == "light") {
*color = "qrc:/index-light.html";
return true;
}
if (color == dark) {
color = ":/index-dark.html";
if (*color == "dark") {
*color = "qrc:/index-dark.html";
return true;
}
return false;
}

void load_args(int argc, char *argv[], QString *file, std::string path,
std::string color) {
void load_args(int argc, char *argv[], QString *file, QString *color) {
// Only file as argument
if (argc == 2) {
if (parse_file(argv[1], file, path))
*color = "qrc:/index-light.html";
if (parse_file(argv[1], file))
return;
die("File not found");
}
Expand All @@ -53,20 +45,20 @@ void load_args(int argc, char *argv[], QString *file, std::string path,
if (argc == 3) {
// colorscheme first, file second
if (argv[1][0] == '-') {
color = argv[1] + 1;
*color = argv[1] + 1;
if (!parse_color(color))
die("Colorscheme not found. Valid colorschemes are: 'light' 'dark'");
if (!parse_file(argv[2], file, path))
if (!parse_file(argv[2], file))
die("File not found");
return;
}

// file first, colorscheme second
if (argv[2][0] == '-') {
color = argv[2] + 1;
*color = argv[2] + 1;
if (!parse_color(color))
die("Colorscheme not found. Valid colorschemes are: 'light' 'dark'");
if (!parse_file(argv[1], file, path))
if (!parse_file(argv[1], file))
die("File not found");
return;
}
Expand All @@ -81,12 +73,10 @@ int main(int argc, char *argv[]) {
QApplication app(argc, argv);

QString file;
std::string path;
std::string color;

load_args(argc, argv, &file, path, color);
QString color;
load_args(argc, argv, &file, &color);

MainWindow window(color, path, &file);
MainWindow window(&color, &file);
window.show();

return app.exec();
Expand Down
16 changes: 7 additions & 9 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
#include <qnamespace.h>
#include <string>

MainWindow::MainWindow(std::string colorscheme, std::string path, QString *file,
QWidget *parent)
MainWindow::MainWindow(QString *colorscheme, QString *file, QWidget *parent)
: QMainWindow(parent), m_ui(new Ui::MainWindow) {
m_ui->setupUi(this);

m_current_path = path;
m_current_path = get_file(file);

m_file = new QFile(*file);

Expand All @@ -35,7 +34,7 @@ MainWindow::MainWindow(std::string colorscheme, std::string path, QString *file,

m_ui->Preview->setPage(m_page);
m_ui->Preview->setContextMenuPolicy(Qt::NoContextMenu);
m_ui->Preview->setUrl(QUrl(colorscheme.c_str()));
m_ui->Preview->setUrl(QUrl(*colorscheme));

loadImages();
loadFile();
Expand Down Expand Up @@ -65,12 +64,12 @@ void MainWindow::loadFile() {
}

void MainWindow::loadImages() {
if (!m_current_path.empty())
res_gen(m_current_path);
if (!m_current_path.isEmpty())
res_gen(m_current_path.toStdString());
else
res_gen(".");

QString qpath = (m_current_path + QRC_FILE).c_str();
QString qpath = m_current_path + QRC_FILE.c_str();

if (file_exists(&qpath)) {
QResource::registerResource(qpath);
Expand All @@ -80,8 +79,7 @@ void MainWindow::loadImages() {

bool MainWindow::setFile(QString path) {
if (file_exists(&path)) {
std::string std_string(path.toStdString());
m_current_path = fix_path(&std_string);
m_current_path = get_file(&path);
m_file->setFileName(path);
loadFile();
return true;
Expand Down
4 changes: 2 additions & 2 deletions src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MainWindow : public QMainWindow {
Q_OBJECT

public:
explicit MainWindow(std::string colorscheme, std::string path, QString *file,
explicit MainWindow(QString *colorscheme, QString *file,
QWidget *parent = nullptr);
~MainWindow();

Expand All @@ -42,7 +42,7 @@ class MainWindow : public QMainWindow {
// Backend
Document m_content;
QString m_current_text;
std::string m_current_path;
QString m_current_path;
QWebChannel *m_channel;
QTimer *m_reload;
QFile *m_file;
Expand Down

0 comments on commit 4f00fa6

Please sign in to comment.