Skip to content

Commit

Permalink
v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
WaleedMortaja committed Mar 26, 2022
1 parent 63c0ba4 commit f24225b
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 18 deletions.
91 changes: 73 additions & 18 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,23 +1,78 @@
# Compiled class file
*.class
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# Log file
*.log
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# BlueJ files
*.ctxt
# AWS User-specific
.idea/**/aws.xml

# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Generated files
.idea/**/contentModel.xml

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# SonarLint plugin
.idea/sonarlint/

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/artifacts/RtlSubtitleCorrector_jar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# RtlSubtitleCorrector
##About
When writing Right-to-Left content inside Left-To-Right context, it is usually mis-represented.
This happens specially with subtitles.
This project fixes this issue by inserting a Right-To-Left isolation character `\u2067` before and after every Left-To-Right sentences.

## Usage
Open cmd or terminal. Run the `jar` file followed by file path:

RtlSubtitleCorrector.jar mysubtitle.srt
Note: You can enter multiple files at once.

RtlSubtitleCorrector.jar mysubtitle1.srt mysubtitle2.srt
For linux Systems, You can use it as:

./RtlSubtitleCorrector.jar mysubtitle.srt

# See also
- [Wikipedia Bidirectional_text](https://en.wikipedia.org/wiki/Bidirectional_text)
- [A similar project](https://github.com/Majid110/SubtitleRtlCorrector)
11 changes: 11 additions & 0 deletions RtlSubtitleCorrector.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
3 changes: 3 additions & 0 deletions src/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: Main

59 changes: 59 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
public static void main(String[] args) {
if (args.length == 0) {
printHelp(System.out);
System.exit(0);
}

for (String inputFilePathString : args) {
Path currentWorkingDirectory = Path.of(System.getProperty("user.dir"));
Path inputFilePath = Path.of(inputFilePathString);
inputFilePath = currentWorkingDirectory.resolve(inputFilePath);

String fileContent = null;
try {
fileContent = Files.readString(inputFilePath);
} catch (IOException e) {
System.out.println("ERROR: Could not read file!");
System.exit(1);
}

String[] inputFileNameAndExtension = Util.separateFileNameAndExtension(inputFilePath.getFileName().toString());

String fixedFileContent = RtlSubtitleCorrector.correctRtl(fileContent, inputFileNameAndExtension[1]);

if (inputFileNameAndExtension[1] == null) {
inputFileNameAndExtension[1] = "txt";
}

String outputParent = inputFilePath.getParent().toString();
String outputFileName = inputFileNameAndExtension[0] + ".RTL." + inputFileNameAndExtension[1];

try {
Files.writeString(Path.of(outputParent, outputFileName), fixedFileContent);
} catch (IOException e) {
System.out.println("ERROR: Could not write file!");
System.exit(1);
}
}

}

private static void printHelp(PrintStream out) {
out.println("Please specify the input file path\n" +
"Example:\n" +
"RtlSubtitleCorrector.jar mysubtitle.srt\n\n" +
"Note: You can enter multiple files at once\n" +
"Example:\n" +
"RtlSubtitleCorrector.jar mysubtitle1.srt mysubtitle2.srt\n\n" +
"For linux Systems, You can use it as:\n" +
"./RtlSubtitleCorrector.jar mysubtitle.srt");
}


}
26 changes: 26 additions & 0 deletions src/RtlSubtitleCorrector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class RtlSubtitleCorrector {
private final static char RTL_CHAR = '\u2067';
private final static String LTR_REGEX = "[\\p{IsLatin}\\p{P}]+";

public static String correctRtl(String text, String fileExtension) {
if (fileExtension == null) {
return defaultRtlCorrection(text);
}

String fixedText = null;
switch (fileExtension) {
case "ssa":
fixedText = text.replaceAll("(\\\\[nN])", RTL_CHAR + "$1" + RTL_CHAR);
fixedText = fixedText.replaceAll("(Dialogue: (?:[^,]*,){9})(.*)", "$1" + RTL_CHAR + "$2");
break;
default:
fixedText = defaultRtlCorrection(text);
break;
}
return fixedText;
}

private static String defaultRtlCorrection(String text) {
return text.replaceAll("(" + LTR_REGEX + ")", RTL_CHAR + "$1" + RTL_CHAR);
}
}
12 changes: 12 additions & 0 deletions src/Util.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class Util {
public static String[] separateFileNameAndExtension(String fileName) {
int dotIndex = fileName.lastIndexOf('.');
if (dotIndex > 0) {
String fileNameWithoutExtension = fileName.substring(0, dotIndex);
String extension = fileName.substring(dotIndex + 1);
return new String[]{fileNameWithoutExtension, extension};
}

return new String[]{fileName, null};
}
}

0 comments on commit f24225b

Please sign in to comment.