Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature added. #19

Merged
merged 5 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ OPTIONS:
processed.
--version Show the version.
-h, --help Show help information.
--config allow for user to store options in json file
```

## Version
Expand All @@ -43,6 +44,7 @@ This commands allows the user to specify the input file present in desktop direc

```swift
swift run ModernSSG --input "your text file"
swift run ModernSSG --config modernSsgConfing.json
```
## Example

Expand Down
Binary file added Sources/.DS_Store
Binary file not shown.
Binary file added Sources/ModernSSG/.DS_Store
Binary file not shown.
203 changes: 195 additions & 8 deletions Sources/ModernSSG/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ extension Command {
}

//input
@Option(name: .shortAndLong ,help:"allow the user to specify an input file or folder to be processed") var input: String
@Option(name: .shortAndLong ,help:"allow the user to specify an input file or folder to be processed") var input: String?

@Option(name: .shortAndLong ,help:"allow the user to specify an input file or folder to be processed") var config: String?

//stylesheet
@Option(name: .shortAndLong, help: "allow the user to optionally specify a URL to a CSS stylesheet.") var stylesheet: String?
Expand All @@ -25,13 +27,60 @@ extension Command {
//lang
@Option(name: .shortAndLong, help:"allow the user to specify the language attribute on root html.") var lang: String?


var result = ""
var new_result = "";
mutating func run() throws{
//Searching the file in desktop directory
if (self.input.hasSuffix(".txt")||self.input.hasSuffix(".md")){
let fileURL = URL(fileURLWithPath: FileManager.default.currentDirectoryPath + "/" + self.input)
let fName: NSString = self.input as NSString




if self.input == nil {

if self.config == nil {
print("Nothing to see here")
}else{
// print("\(self.config!)")
}

let fileURL = URL(fileURLWithPath: FileManager.default.currentDirectoryPath + "/" + self.config!)
// print(fileURL)
let text2 = try String(contentsOf: fileURL, encoding: .utf8)
// print(text2)
let myStrings = text2.components(separatedBy: .newlines)
// TextView.text = myStrings.joined(separator: ", ")

// myStrings[1].replacingOccurrences(
// of:"i",
// with: "a",

// options: .regularExpression
// )


let index = myStrings[1].index(myStrings[1].endIndex, offsetBy: -10)
let str = myStrings[1][index...] // playground

let last = str.prefix(9)
print( last)

// if let dotRange = myStrings[1].range(of: ":") {
// myStrings[1].removeSubrange(dotRange.lowerBound..<myStrings[1].endIndex)
// print(myStrings[1])
// }

// myStrings[1] = myStrings[1].components(separatedBy: ":")[0]
// print( myStrings[1])




// print("OKKKKK")


if (last.hasSuffix(".txt")||last.hasSuffix(".md")){
let fileURL = URL(fileURLWithPath: FileManager.default.currentDirectoryPath + "/" + last)
let fName: NSString = last as NSString
let pathPrefix = fName.deletingPathExtension
print("File URL to read \(fileURL)")

Expand All @@ -40,11 +89,11 @@ extension Command {
//!!!!!!!!!!!!!!!!
result = try String(contentsOf: fileURL, encoding: .utf8)

if(self.input.hasSuffix(".txt")) {
if(last.hasSuffix(".txt")) {
result = result.replacingOccurrences(of: "\r?\n\r?\n\r?\n", with: "<p>", options: .regularExpression)
result = result.replacingOccurrences(of: "\r?\n\r?\n", with: "</p><p>", options: .regularExpression)
//MD support - italics
} else if(self.input.hasSuffix(".md")){
} else if(self.input!.hasSuffix(".md")){
result = result.replacingOccurrences(of: " _", with: "<i>", options: .regularExpression)
result = result.replacingOccurrences(of: "_ ", with: "</i>", options: .regularExpression)
//Add support for inline <code> blocks //19
Expand Down Expand Up @@ -90,7 +139,7 @@ extension Command {


else{
let url = URL(fileURLWithPath: FileManager.default.currentDirectoryPath + "/\(self.input)/")
let url = URL(fileURLWithPath: FileManager.default.currentDirectoryPath + "/\(self.input!)/")
var files = [URL]()
//Searching all text files in desktop directory
if let enumerator = FileManager.default.enumerator(at: url, includingPropertiesForKeys: [.isRegularFileKey], options: [.skipsHiddenFiles, .skipsPackageDescendants]) { for case let fileURL as URL in enumerator {
Expand Down Expand Up @@ -148,6 +197,144 @@ extension Command {
}
}
}















}else{
if (self.input!.hasSuffix(".txt")||self.input!.hasSuffix(".md")){
let fileURL = URL(fileURLWithPath: FileManager.default.currentDirectoryPath + "/" + self.input!)
let fName: NSString = self.input! as NSString
let pathPrefix = fName.deletingPathExtension
print("File URL to read \(fileURL)")

do {
//Reading the data
//!!!!!!!!!!!!!!!!
result = try String(contentsOf: fileURL, encoding: .utf8)

if(self.input!.hasSuffix(".txt")) {
result = result.replacingOccurrences(of: "\r?\n\r?\n\r?\n", with: "<p>", options: .regularExpression)
result = result.replacingOccurrences(of: "\r?\n\r?\n", with: "</p><p>", options: .regularExpression)
//MD support - italics
} else if(self.input!.hasSuffix(".md")){
result = result.replacingOccurrences(of: " _", with: "<i>", options: .regularExpression)
result = result.replacingOccurrences(of: "_ ", with: "</i>", options: .regularExpression)
//Add support for inline <code> blocks //19
result = result.replacingOccurrences(of: "\r?\n`", with: "<code>", options: .regularExpression)
result = result.replacingOccurrences(of: "`\r?\n", with: "</code>", options: .regularExpression)
//Add support for a horizontal rule in Markdown
result = result.replacingOccurrences(of: "---", with: "<hr>", options: .regularExpression)
}
result = result.replacingOccurrences(of: pathPrefix, with: " ", options: .regularExpression)
print("File data copied")

//Writing the data
//!!!!!!!!!!!!!!!!
let storedText =
"<!doctype html><html lang = \(self.lang ?? "en-CA")><head><LINK rel='stylesheet' href=\(self.stylesheet ?? " ")><meta charset='utf-8'><title>\(pathPrefix)</title><meta name='viewport' content='width=device-width, initial-scale=1'></head><body><h1>\(pathPrefix)</h1>\(result)</body></html>";

if (self.output != nil) {
let filePath = FileManager.default.currentDirectoryPath + "/ \(self.output ?? "" )/\(pathPrefix).html"
if (FileManager.default.createFile(atPath: filePath, contents: nil, attributes: nil))
{
try storedText.write(toFile: filePath, atomically: false, encoding: .utf8)
print("File Created at \(filePath)")
}
else{
print("Error in writing data! Please create folder in current directory");
}
}
else{
let filePath = FileManager.default.currentDirectoryPath + "/Dist/\(pathPrefix).html"
if (FileManager.default.createFile(atPath: filePath, contents: nil, attributes: nil))
{
try storedText.write(toFile: filePath, atomically: false, encoding: .utf8)
print("File Created at \(filePath)")
}
else{
print("Error in writing data! Please create dist folder if missing.");
print("Error in writing data! Please create folder in current directory");
}
}
}
catch {print("Error in reading file. Please check name and extension of file. *** File must be in current directory ***")}
}


else{
let url = URL(fileURLWithPath: FileManager.default.currentDirectoryPath + "/\(self.input!)/")
var files = [URL]()
//Searching all text files in desktop directory
if let enumerator = FileManager.default.enumerator(at: url, includingPropertiesForKeys: [.isRegularFileKey], options: [.skipsHiddenFiles, .skipsPackageDescendants]) { for case let fileURL as URL in enumerator {
do {
let fileAttributes = try fileURL.resourceValues(forKeys:[.isRegularFileKey])
if fileAttributes.isRegularFile! {
new_result = try String(contentsOf: fileURL, encoding: .utf8)
new_result = new_result.replacingOccurrences(of: "\r?\n\r?\n\r?\n", with: "<p>", options: .regularExpression)
new_result = new_result.replacingOccurrences(of: "\r?\n\r?\n", with: "</p><p>", options: .regularExpression)
files.append(fileURL)
}
}catch { print(error, fileURL) }
}
}
//
for i in 0..<files.count {
let fileName = files[i].deletingPathExtension().lastPathComponent
if (files[i].lastPathComponent.hasSuffix(".txt")){
do {
//Reading the data
//!!!!!!!!!!!!!!!!
new_result = try String(contentsOf: files[i], encoding: .utf8)
new_result = new_result.replacingOccurrences(of: "\n\n\n", with: "<p>", options: .regularExpression)
new_result = new_result.replacingOccurrences(of: "\n\n", with: "</p><p>", options: .regularExpression)
new_result = new_result.replacingOccurrences(of: fileName, with: "", options: .regularExpression)
print("File data copied")

//Writing the data
//!!!!!!!!!!!!!!!!
let storedText = "<!doctype html><html lang =\(self.lang ?? "en-CA")><head><LINK rel='stylesheet' href=\(self.stylesheet ?? " ")><meta charset='utf-8'><title>\(fileName)</title><meta name='viewport' content='width=device-width,initial-scale=1'></head><body><h1>\(fileName)</h1>\(new_result)</body></html>";
if (self.output != nil) {
let filePath = FileManager.default.currentDirectoryPath + "/\(self.output ?? " ")/\(fileName).html"
if (FileManager.default.createFile(atPath: filePath, contents: nil, attributes: nil))
{
try storedText.write(toFile: filePath, atomically: false, encoding: .utf8)
print("File Created at \(filePath)")
}
else{
print("Error in writing data! Please create folder in current directory");
}
}
else{
let filePath = FileManager.default.currentDirectoryPath + "/Dist/\(fileName).html"
if (FileManager.default.createFile(atPath: filePath, contents: nil, attributes: nil))
{
try storedText.write(toFile: filePath, atomically: false, encoding: .utf8)
print("File Created at \(filePath)")
}
else{
print("Error in writing data! Please create folder in current directory");
}
}
}
catch {print("Error in reading folder. Please check name and location of folder. *** Folder must be in current directory ***")}
}
}
}
}
//Searching the file in desktop directory

}
}
}
Expand Down
9 changes: 9 additions & 0 deletions index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Welcome to My Static Site
author: JavaScript Joel
date: Aug 15, 2020
---

👋 Welcome fellow Tailwind CSS and miminal monochrome blog fan. This starter template provides a starting point to create your own minimal monochrome blog using Tailwind CSS and vanilla Javascript.

OKASKSDLSADFKDSFPKFSDPLFSDFKJ
3 changes: 3 additions & 0 deletions modernSsgConfing.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"input": "index.txt"
}