Skip to content
This repository has been archived by the owner on Nov 7, 2022. It is now read-only.

Commit

Permalink
feat: preferences panel (#61)
Browse files Browse the repository at this point in the history
* Updated version number to 0.5.0

* Add preferences panel

* Updated greetings.yml

* Added HTTP tab in the preferences dialogue

* Updated README.md
  • Loading branch information
blaisewang authored Feb 14, 2020
1 parent 392157e commit 14453f7
Show file tree
Hide file tree
Showing 24 changed files with 912 additions and 429 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/greetings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ jobs:
- uses: actions/first-interaction@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
issue-message: '[Auto response] Sorry for your first issue in this repo. We will review it ASAP.'
pr-message: '[Auto response] Welcome! Your first pr in this repo. We will review it ASAP.'
issue-message: '[Auto response] Looks like this is your first issue. I will look into it as soon as possible.'
pr-message: '[Auto response] Welcome! Your first pr for this project. I will review it as soon as possible.'
12 changes: 5 additions & 7 deletions README-ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ Image2LaTeX 是空闲时间的个人项目,仅保留了 Snip 的核心功能

### 新功能

- 适用于 macOS 的独立可执行应用程序.
-`Preferences` 面板。
- 第三和第四个结果的多种格式选项。
- 支持使用 HTTP 代理发出 OCR 请求。

## 使用要求

Expand All @@ -34,9 +36,9 @@ Image2LaTeX 是空闲时间的个人项目,仅保留了 Snip 的核心功能

在程序第一次启动时输入你的 API keys(不含单引号)。

macOS 和 Windows 用户可以在系统托盘找到 `API Credentials` 选项来修改你的 API keys。
macOS 和 Windows 用户可以在系统托盘找到 `Preferences` 菜单项中的 `API Credentials` 来修改你的 API keys。

Linux 用户可以通过在应用内任意位置右键单击的方式打开 `API Credentials` 对话框。
Linux 用户可以通过在应用内任意位置右键单击的方式打开 `Preferences` 对话框。

**注意,**,应用不会将你的 API keys 发送给除 MathpixOCR API 服务器之外的任何人(查看 [OCRRequestHelper.java](https://github.com/blaisewang/img2latex-mathpix/blob/master/src/main/java/io/OCRRequestHelper.java) 了解你的 API keys 是如何被使用的)。

Expand Down Expand Up @@ -91,10 +93,6 @@ java -jar Image2LaTeX-x.x.x.jar

查看GitHub这篇关于关注和取消关注仓库的发行版的[文章](https://help.github.com/cn/github/receiving-notifications-about-activity-on-github/watching-and-unwatching-releases-for-a-repository)

### 开发计划

- 增加多种结果格式选项。

## 问题

当遇到错误时,请首先阅读 Mathpix OCR [API 文档](https://docs.mathpix.com/#error-id-types) 中关于 Error Types 章节以及[已报告的问题](https://github.com/blaisewang/img2latex-mathpix/issues)
Expand Down
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ See the [features](https://mathpix.com/ocr#features) section on MathpixOCR websi

### New Features

- Self-contained executable application for macOS.
- New `Preferences` panel.
- Multiple formatting options for 3rd and 4th results.
- HTTP proxy support for making OCR request.

## Requirements

Expand All @@ -33,9 +35,9 @@ See the [features](https://mathpix.com/ocr#features) section on MathpixOCR websi

Enter your API keys (without single quotes) at the first launch of this app.

To change your API keys, macOS and Windows users will find a menu item called `API Credentials` by clicking this app's menu bar icon.
To change your API keys, macOS and Windows users will find the `API Credentials` tab in the `Preferences` menu item by clicking this app's menu bar icon.

Linux users can open the `API Credentials` dialogue by right-clicking anywhere inside the application.
Linux users can open the `Preferences` panel by right-clicking anywhere inside the application.

**Note**, this app will not send your API keys to others except for MathpixOCR API server (see [OCRRequestHelper.java](https://github.com/blaisewang/img2latex-mathpix/blob/master/src/main/java/io/OCRRequestHelper.java) to learn how your API keys are used).

Expand Down Expand Up @@ -94,10 +96,6 @@ Therefore, a smaller size Fat JAR `Image2LaTeX-x.x.x.jar` has also released.

See this [article](https://help.github.com/en/github/receiving-notifications-about-activity-on-github/watching-and-unwatching-releases-for-a-repository) about watching and unwatching releases for a repository by GitHub.

### Development plans

- Add multiple result formatting option.

## Issues

Please first refer to the official [API Docs](https://docs.mathpix.com/#error-id-types) about error types and the [reported issue](https://github.com/blaisewang/img2latex-mathpix/issues) if you encountered an error.
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group "blaise.img2latex"
version "0.4.3"
version "0.5.0"

compileJava {
sourceCompatibility = 11
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/entry/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package entry;

import io.IOUtils;
import org.apache.commons.lang3.SystemUtils;
import ui.App;

import java.awt.Toolkit;
import java.io.IOException;
Expand All @@ -26,7 +26,7 @@ public static void main(String[] args) throws IOException {
}

// run JavaFX application
ui.MainAPP.main(args);
App.main(args);

}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/io/APICredentialConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io;


/**
* IO.APICredentialConfig.java
* used to store app_id and id_key.
Expand Down
194 changes: 183 additions & 11 deletions src/main/java/io/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import javafx.scene.image.Image;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
Expand All @@ -29,8 +30,25 @@
*/
public class IOUtils {


public final static String UNKNOWN_HOST_ERROR = "Unknown host";
public final static String CONNECTION_REFUSED_ERROR = "Connection refused";
public final static String INVALID_CREDENTIALS_ERROR = "Invalid credentials";
public final static String INVALID_PROXY_CONFIG_ERROR = "Invalid proxy config";
public final static String SSL_PEER_SHUT_DOWN_INCORRECTLY_ERROR = "SSL peer shut down incorrectly";

public final static String OCR_API_URL = "https://api.mathpix.com/v3/latex";
public final static String I2L_GITHUB_RELEASES_URL = "https://github.com/blaisewang/img2latex-mathpix/releases";

private final static String I2L_LATEST_RELEASE_API_URL = "https://api.github.com/repos/blaisewang/img2latex-mathpix/releases/latest";

private final static String I2L_APP_ID = "I2L_APP_ID";
private final static String I2L_APP_KEY = "I2L_APP_KEY";
private final static String I2L_THIRD_RESULT_WRAPPER = "I2L_THIRD_RESULT_WRAPPER";
private final static String I2L_FOURTH_RESULT_WRAPPER = "I2L_FOURTH_RESULT_WRAPPER";
private final static String I2L_PROXY_ENABLED = "I2L_PROXY_ENABLED";
private final static String I2L_PROXY_HOSTNAME = "I2L_PROXY_HOSTNAME";
private final static String I2L_PROXY_PORT = "I2L_PROXY_PORT";

private final static String CONFIG_NODE_PATH = "I2L_API_CREDENTIAL_CONFIG";
private static Preferences preferences = Preferences.userRoot().node(CONFIG_NODE_PATH);
Expand Down Expand Up @@ -92,14 +110,24 @@ public static String getLatestVersion() {
SSLContext context = SSLContexts.createSystemDefault();
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(context, SUPPORTED_PROTOCOLS, null, NoopHostnameVerifier.INSTANCE);

// maximum connection waiting time 1 seconds
// maximum connection waiting time 1 second
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(1000).build();

if (IOUtils.getProxyEnabled()) {
// proxy enabled
ProxyConfig proxyConfig = IOUtils.getProxyConfig();
if (proxyConfig.isValid()) {
HttpHost proxy = new HttpHost(proxyConfig.getHostname(), proxyConfig.getPort());
// maximum connection waiting time 1 second
requestConfig = RequestConfig.custom().setConnectTimeout(1000).setProxy(proxy).build();
}
}

// build the HTTP client with above config
CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).setSSLSocketFactory(sslConnectionSocketFactory).build();

// API url
HttpGet request = new HttpGet("https://api.github.com/repos/blaisewang/img2latex-mathpix/releases/latest");
HttpGet request = new HttpGet(I2L_LATEST_RELEASE_API_URL);

try {

Expand Down Expand Up @@ -127,31 +155,175 @@ public static String getLatestVersion() {
* @return whether the API credential config is valid.
*/
public static boolean isAPICredentialConfigValid() {
// default empty String
String appId = preferences.get(I2L_APP_ID, "");
String appKey = preferences.get(I2L_APP_KEY, "");
return getAPICredentialConfig().isValid();
}

return !"".equals(appId) && !"".equals(appKey);
/**
* Set app ID.
*
* @param appId App ID to be written.
*/
public static void setAppId(String appId) {
preferences.put(I2L_APP_ID, appId);
}

/**
* Set API credential config with given App ID and App Key.
* Set app key.
*
* @param appId App ID to be written.
* @param appKey App key to be written.
*/
public static void setAPICredentialConfig(String appId, String appKey) {
preferences.put(I2L_APP_ID, appId);
public static void setAppKey(String appKey) {
preferences.put(I2L_APP_KEY, appKey);
}

/**
* Read App ID and App Key from Java Preferences API.
* Get App ID and App Key from Java Preferences API.
*
* @return IO.APICredentialConfig object.
*/
public static APICredentialConfig getAPICredentialConfig() {
return new APICredentialConfig(preferences.get(I2L_APP_ID, ""), preferences.get(I2L_APP_KEY, ""));
}

/**
* Set third result wrapper option.
*
* @param option option to be written.
*/
public static void setThirdResultWrapper(int option) {
preferences.putInt(I2L_THIRD_RESULT_WRAPPER, option);
}

/**
* Get third result wrapper option.
*
* @return third result wrapper option.
*/
public static int getThirdResultWrapper() {
return preferences.getInt(I2L_THIRD_RESULT_WRAPPER, 2);
}

/**
* Set fourth result wrapper option.
*
* @param option option to be written.
*/
public static void setFourthResultWrapper(int option) {
preferences.putInt(I2L_FOURTH_RESULT_WRAPPER, option);
}

/**
* Get fourth result wrapper option.
*
* @return fourth result wrapper option.
*/
public static int getFourthResultWrapper() {
return preferences.getInt(I2L_FOURTH_RESULT_WRAPPER, 0);
}

/**
* Wrap the original recognised result with the selected formatting options.
*
* @param result recognised result.
* @return the wrapped result.
*/
public static String thirdResultWrapper(String result) {

// return null if the original result is null
if (result == null) {
return null;
}

int option = getThirdResultWrapper();

switch (option) {
case 0:
return "\\begin{equation*}\n " + result + " \n\\end{equation*}";
case 1:
return "\\begin{align*}\n " + result + " \n\\end{align*}";
case 3:
return "\\[\n " + result + " \n\\]";
default:
// default for option 2 and others
return "$$\n " + result + " \n$$";
}

}

/**
* Wrap the original recognised result with the selected formatting options.
*
* @param result recognised result.
* @return the wrapped result.
*/
public static String fourthResultWrapper(String result) {

// return null if the original result is null
if (result == null) {
return null;
}

int option = getFourthResultWrapper();

if (option == 1) {
return "\\begin{align}\n " + result + " \n\\end{align}";
}

return "\\begin{equation}\n " + result + " \n\\end{equation}";

}

/**
* Set using proxy or not.
*
* @param option option to be written.
*/
public static void setProxyEnabled(boolean option) {
preferences.putBoolean(I2L_PROXY_ENABLED, option);
}

/**
* Get using proxy or not.
*
* @return proxy enabled option.
*/
public static boolean getProxyEnabled() {
return preferences.getBoolean(I2L_PROXY_ENABLED, false);
}

/**
* Set proxy host.
*
* @param host host to be written.
*/
public static void setProxyHostname(String host) {
preferences.put(I2L_PROXY_HOSTNAME, host);
}

/**
* Set proxy port.
*
* @param port port to be written.
*/
public static void setProxyPort(String port) {
preferences.put(I2L_PROXY_PORT, port);
}

/**
* Get proxy config.
*
* @return proxy config.
*/
public static ProxyConfig getProxyConfig() {

int port;
try {
port = Integer.parseInt(preferences.get(I2L_PROXY_PORT, ""));
} catch (NumberFormatException e) {
port = -1;
}

return new ProxyConfig(preferences.get(I2L_PROXY_HOSTNAME, ""), port);
}

}
Loading

0 comments on commit 14453f7

Please sign in to comment.