Skip to content

Commit

Permalink
added support for multiple rule files and bundled rssbud rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Cay-Zhang committed Jul 10, 2022
1 parent 508b1de commit c2a0220
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 11 deletions.
10 changes: 10 additions & 0 deletions RSSBud.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@
EE7CD91224DD547200FAA837 /* Core.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE000A9524B41E9600979B1A /* Core.swift */; };
EE7CD91524DD547200FAA837 /* radar-rules.js in Resources */ = {isa = PBXBuildFile; fileRef = EE000A8624B41E1000979B1A /* radar-rules.js */; };
EE7CD91E24DD6FA000FAA837 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEB7579324B2B7C600F26CF7 /* Extensions.swift */; };
EEA11F152844906C003F839B /* rssbud-rules.js in Resources */ = {isa = PBXBuildFile; fileRef = EEA11F142844906C003F839B /* rssbud-rules.js */; };
EEA11F162844906C003F839B /* rssbud-rules.js in Resources */ = {isa = PBXBuildFile; fileRef = EEA11F142844906C003F839B /* rssbud-rules.js */; };
EEA11F172844906C003F839B /* rssbud-rules.js in Resources */ = {isa = PBXBuildFile; fileRef = EEA11F142844906C003F839B /* rssbud-rules.js */; };
EEA11F182844906C003F839B /* rssbud-rules.js in Resources */ = {isa = PBXBuildFile; fileRef = EEA11F142844906C003F839B /* rssbud-rules.js */; };
EEA6AF8926FAA4720044166E /* ErrorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEA6AF8826FAA4720044166E /* ErrorView.swift */; };
EEA6AF8A26FAA4720044166E /* ErrorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEA6AF8826FAA4720044166E /* ErrorView.swift */; };
EEA6AF8B26FAA4720044166E /* ErrorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEA6AF8826FAA4720044166E /* ErrorView.swift */; };
Expand Down Expand Up @@ -187,6 +191,7 @@
EE7CD90624DD541100FAA837 /* RSSBudTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RSSBudTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
EE7CD90824DD541100FAA837 /* RSSBudTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RSSBudTests.swift; sourceTree = "<group>"; };
EE7CD90A24DD541100FAA837 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
EEA11F142844906C003F839B /* rssbud-rules.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = "rssbud-rules.js"; sourceTree = "<group>"; };
EEA6AF8826FAA4720044166E /* ErrorView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ErrorView.swift; sourceTree = "<group>"; };
EEB7577224B2B6E600F26CF7 /* App.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = App.swift; sourceTree = "<group>"; };
EEB7577324B2B6E600F26CF7 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -290,6 +295,7 @@
isa = PBXGroup;
children = (
EE000A8624B41E1000979B1A /* radar-rules.js */,
EEA11F142844906C003F839B /* rssbud-rules.js */,
EE000A9524B41E9600979B1A /* Core.swift */,
1CEB9FE3260E525600694262 /* core.min.js */,
);
Expand Down Expand Up @@ -585,6 +591,7 @@
files = (
EE7CD91524DD547200FAA837 /* radar-rules.js in Resources */,
1CEB9FE6260E525600694262 /* core.min.js in Resources */,
EEA11F172844906C003F839B /* rssbud-rules.js in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -595,6 +602,7 @@
1C270114250B5BC300595BCE /* LaunchScreen.storyboard in Resources */,
1C32219E25192F6A00CD9F56 /* Localizable.strings in Resources */,
1C6EE75D258CA78900C645F0 /* shortcut-workshop.json in Resources */,
EEA11F152844906C003F839B /* rssbud-rules.js in Resources */,
EEB7578924B2B6E700F26CF7 /* Assets.xcassets in Resources */,
EE000A8B24B41E1000979B1A /* radar-rules.js in Resources */,
1CEB9FE4260E525600694262 /* core.min.js in Resources */,
Expand All @@ -610,6 +618,7 @@
1CEB9FE5260E525600694262 /* core.min.js in Resources */,
EEB7578A24B2B6E700F26CF7 /* Assets.xcassets in Resources */,
EE000A8C24B41E1000979B1A /* radar-rules.js in Resources */,
EEA11F162844906C003F839B /* rssbud-rules.js in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -622,6 +631,7 @@
1CCFA7AD268DE6730011190F /* JavaScriptPreprocessingFile.js in Resources */,
EEC4A4FD24E3D032005041CA /* Media.xcassets in Resources */,
1CDB36F0254AEA74002E84D8 /* Assets.xcassets in Resources */,
EEA11F182844906C003F839B /* rssbud-rules.js in Resources */,
1C6EE75F258CA78900C645F0 /* shortcut-workshop.json in Resources */,
1CEB9FE7260E525600694262 /* core.min.js in Resources */,
);
Expand Down
24 changes: 20 additions & 4 deletions Shared/Core/Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@ enum Core {
return url
}()

static let localRules = try! PersistentFile(
static let localRadarRuleFile = try! PersistentFile(
url: Core.directoryURL.appendingPathComponent("radar-rules.js", isDirectory: false),
defaultContentURL: Bundle.main.url(forResource: "radar-rules", withExtension: "js")!
)

static let localRSSBudRuleFile = try! PersistentFile(
url: Core.directoryURL.appendingPathComponent("rssbud-rules.js", isDirectory: false),
defaultContentURL: Bundle.main.url(forResource: "rssbud-rules", withExtension: "js")!
)

static let onFinishReloadingRules = ObservableObjectPublisher()

static let jsContext: JSContext = {
Expand All @@ -53,7 +58,18 @@ enum Core {
}

// Load Rules
_ = context.evaluateScript(localRules.content)
_ = context.evaluateScript(localRadarRuleFile.content)

_ = context.evaluateScript("""
const ruleFiles = new Map();
ruleFiles.set("radar-rules", rules);
""")

context.setObject(context.evaluateScript(localRSSBudRuleFile.content), forKeyedSubscript: "rules" as NSString)

_ = context.evaluateScript("""
ruleFiles.set("rssbud-rules", rules);
""")

// Polyfills
context.setObject(context.globalObject, forKeyedSubscript: "window" as NSString)
Expand All @@ -73,7 +89,7 @@ enum Core {
""")

// Reload Rules on Changes
localRules.contentPublisher
localRadarRuleFile.contentPublisher
.dropFirst()
.debounce(for: 0.5, scheduler: DispatchQueue.main)
.removeDuplicates()
Expand Down Expand Up @@ -113,7 +129,7 @@ enum Core {
JSON.stringify(core.analyze(
"\(url.string ?? "")",
html,
rules
ruleFiles
));
"""
)!.toString()!
Expand Down
61 changes: 61 additions & 0 deletions Shared/Core/rssbud-rules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
({
"github.com": {
_name: "GitHub",
".": [
{
title: "仓库 Releases",
source: ["/:user/:repo/releases", "/:user/:repo/releases/*", "/:user/:repo"],
targetType: "pathForOriginal",
target: "/:user/:repo/releases.atom"
},
{
title: "仓库 Commits",
source: ["/:user/:repo/commits", "/:user/:repo/commits/:branch", "/:user/:repo"],
targetType: "pathForOriginal",
target: ({ user, repo, branch }, url) => `/${user}/${repo}/commits${branch ? "/" + branch : ""}.atom`
},
{
title: "用户动态",
source: "/:user",
targetType: "pathForOriginal",
target: "/:user.atom"
}
]
},
"reddit.com": {
_name: "Reddit",
".": [
{
title: "当前页面",
targetType: "url",
target: (params, url) => {
url = new URL(url);
url.pathname += ".rss";
return url;
}
},
{
title: "当前页面 (trashhalo/reddit-rss)",
docs: "https://github.com/trashhalo/reddit-rss",
targetType: "url",
target: (params, url) => {
url = new URL(url);
url.pathname += ".json";
url.domain = "reddit.0qz.fun";
return url;
}
}
]
},
"cnn.com": {
_name: "CNN",
".": [
{
title: "Top Stories",
docs: "https://www.cnn.com/services/rss/",
targetType: "url",
target: "http://rss.cnn.com/rss/cnn_topstories.rss"
}
]
}
})
12 changes: 9 additions & 3 deletions Shared/Core/src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,23 @@ function getList(data) {
return rules;
}

function analyze(url, html, ruleFile) {
function analyze(url, html, ruleFiles) {
let rssFeedsFromHTML = [];
let debugInfo = "";
if (html) {
const document = (new JSDOM(html, { url })).window.document;
debugInfo = document.location.href;
rssFeedsFromHTML = getPageRSS(document);
}
const { rssFeeds: rssFeedsFromRules, rssHubFeeds } = getPageRSSHub({ url, html, ruleFile });
const rssFeedsFromRuleFiles = [];
const rssHubFeeds = [];
for (const [ruleFileName, ruleFile] of ruleFiles) {
const { rssFeeds: rssFeedsFromRuleFile, rssHubFeeds: rssHubFeedsFromRuleFile } = getPageRSSHub({ url, html, ruleFile });
rssFeedsFromRuleFiles.push(...rssFeedsFromRuleFile);
rssHubFeeds.push(...rssHubFeedsFromRuleFile);
}
return {
rssFeeds: rssFeedsFromRules.concat(rssFeedsFromHTML), rssHubFeeds, debugInfo
rssFeeds: rssFeedsFromRuleFiles.concat(rssFeedsFromHTML), rssHubFeeds, debugInfo
};
}

Expand Down
6 changes: 3 additions & 3 deletions Shared/RuleManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class RuleManager: ObservableObject {
var cancelBag = Set<AnyCancellable>()

init() {
Core.localRules.contentPublisher
Core.localRadarRuleFile.contentPublisher
.dropFirst()
.map { _ in Date() }
.receive(on: DispatchQueue.main)
Expand All @@ -39,7 +39,7 @@ class RuleManager: ObservableObject {
print(error)
}
} receiveValue: { [weak self] string in
Core.localRules.content = string
Core.localRadarRuleFile.content = string
DispatchQueue.main.async {
withAnimation {
self?.isFetchingRemoteRules = false
Expand Down Expand Up @@ -69,7 +69,7 @@ class RuleManager: ObservableObject {
task.setTaskCompleted(success: false)
}
} receiveValue: { [weak self] string in
Core.localRules.content = string
Core.localRadarRuleFile.content = string
DispatchQueue.main.async {
withAnimation {
self?.isFetchingRemoteRules = false
Expand Down
2 changes: 1 addition & 1 deletion Shared/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ struct IntegrationSettingsView: View {

extension Core {
struct RulesEditor: View {
@State var rules: String = Core.localRules.content
@State var rules: String = Core.localRadarRuleFile.content

var body: some View {
TextEditor(text: $rules)
Expand Down

0 comments on commit c2a0220

Please sign in to comment.