<commit_before>--- === plugins.finalcutpro.toolbox.titlestokeywords ===
---
--- Converts Titles to Keywords

local require                   = require

local hs                        = _G.hs

local log                       = require "hs.logger".new "titlestokeywords"

local dialog                    = require "hs.dialog"
local fs                        = require "hs.fs"
local image                     = require "hs.image"
local inspect                   = require "hs.inspect"

local config                    = require "cp.config"
local fcp                       = require "cp.apple.finalcutpro"
local fcpxml                    = require "cp.apple.fcpxml"
local fn                        = require "cp.fn"
local fntable                   = require "cp.fn.table"
local fnvalue                   = require "cp.fn.value"
local i18n                      = require "cp.i18n"
local time                      = require "cp.apple.fcpxml.time"
local tools                     = require "cp.tools"

local xml                       = require "hs._asm.xml"

local between                   = tools.between
local chain                     = fn.chain
local escapeTilda               = tools.escapeTilda
local get                       = fntable.get
local is                        = fnvalue.is
local lines                     = tools.lines
local pipe                      = fn.pipe
local replace                   = tools.replace
local tableContains             = tools.tableContains
local tableCount                = tools.tableCount
local trim                      = tools.trim
local urlFromPath               = fs.urlFromPath
local webviewAlert              = dialog.webviewAlert
local writeToFile               = tools.writeToFile

local mod = {}

-- ALTERNATIVE_COMMA -> string
-- Constant
-- An alternative comma to use to avoid a FCPXML bug.
local ALTERNATIVE_COMMA = "‚" -- NOTE: This isn't actually a "normal" comma, even though it looks like one in some fonts.

--- plugins.finalcutpro.toolbox.titlestokeywords.addSpaceAfterSequence <cp.prop: boolean>
--- Field
--- Add Space After Sequence
mod.addSpaceAfterSequence = config.prop("toolbox.titlestokeywords.addSpaceAfterSequence", true)

--- plugins.finalcutpro.toolbox.titlestokeywords.treatFavoriteAndRejectAsRatingsInsteadOfKeywords <cp.prop: boolean>
--- Field
--- Treat FAVORITE and REJECT as ratings instead of keywords
mod.treatFavoriteAndRejectAsRatingsInsteadOfKeywords = config.prop("toolbox.titlestokeywords.treatFavoriteAndRejectAsRatingsInsteadOfKeywords", true)

--- plugins.finalcutpro.toolbox.titlestokeywords.replaceCommasWithAlternativeCommas <cp.prop: boolean>
--- Field
--- Remove Project from Event
mod.replaceCommasWithAlternativeCommas = config.prop("toolbox.titlestokeywords.replaceCommasWithAlternativeCommas", true)

--- plugins.finalcutpro.toolbox.titlestokeywords.removeProjectFromEvent <cp.prop: boolean>
--- Field
--- Remove Project from Event
mod.removeProjectFromEvent = config.prop("toolbox.titlestokeywords.removeProjectFromEvent", true)

--- plugins.finalcutpro.toolbox.titlestokeywords.mergeWithExistingEvent <cp.prop: boolean>
--- Field
--- Merge With Existing Event?
mod.mergeWithExistingEvent = config.prop("toolbox.titlestokeywords.mergeWithExistingEvent", false)

--- plugins.finalcutpro.toolbox.titlestokeywords.useTitleContentsInsteadOfTitleName <cp.prop: boolean>
--- Field
--- Use Title Contents Instead of Title Name?
mod.useTitleContentsInsteadOfTitleName = config.prop("toolbox.titlestokeywords.useTitleContentsInsteadOfTitleName", false)

--- plugins.finalcutpro.toolbox.titlestokeywords.textEditor <cp.prop: string>
--- Field
--- Last Text Editor Value
mod.textEditor = config.prop("toolbox.titlestokeywords.textEditor", "")

--- plugins.finalcutpro.toolbox.titlestokeywords.textEditorTwo <cp.prop: string>
--- Field
--- Last Text Editor Two Value
mod.textEditorTwo = config.prop("toolbox.titlestokeywords.textEditorTwo", "")

--- plugins.finalcutpro.toolbox.titlestokeywords.prefix <cp.prop: string>
--- Field
--- Last Prefix Value
mod.prefix = config.prop("toolbox.titlestokeywords.prefix", " - ")

--- plugins.finalcutpro.toolbox.titlestokeywords.suffix <cp.prop: string>
--- Field
--- Last Suffix Value
mod.suffix = config.prop("toolbox.titlestokeywords.suffix", " - ")

--- plugins.finalcutpro.toolbox.titlestokeywords.startOrEnd <cp.prop: string>
--- Field
--- Last Start or End Value
mod.startOrEnd = config.prop("toolbox.titlestokeywords.startOrEnd", "start")

--- plugins.finalcutpro.toolbox.titlestokeywords.startWith <cp.prop: string>
--- Field
--- Last Start With Value
mod.startWith = config.prop("toolbox.titlestokeywords.startWith", "1")

--- plugins.finalcutpro.toolbox.titlestokeywords.duration <cp.prop: string>
--- Field
--- Duration of the titles created by "Create Titles from Text"
mod.duration = config.prop("toolbox.titlestokeywords.duration", "5")

--- plugins.finalcutpro.toolbox.titlestokeywords.stepValue <cp.prop: string>
--- Field
--- Last Step With Value
mod.stepValue = config.prop("toolbox.titlestokeywords.stepValue", "1")

--- plugins.finalcutpro.toolbox.titlestokeywords.padding <cp.prop: string>
--- Field
--- Last Padding Value
mod.padding = config.prop("toolbox.titlestokeywords.padding", "0")

---------------------------------------------------
-- HELPER FUNCTIONS:
---------------------------------------------------

-- xPathQuery(xPath) -> function(node) -> table
-- Function
-- A function combinator that takes an XPath query and returns a function that takes
-- a node and returns the result of the query, usually a table with one or more values.
--
-- Parameters:
--  * xPath - The XPath query.
--
-- Returns:
--  * The function.
local function xPathQuery(xPath)
    return function(node)
        return node:XPathQuery(xPath)
    end
end

-- xPath(value) -> function(node) -> anything | nil
-- Function
-- A function combinator that takes an XPath query and returns a chain function that takes
-- a node and returns the first result of the query, or `nil` if nothing was found.
--
-- Parameters:
--  * xPath - The XPath query.
--
-- Returns:
--  * The chain function.
local function xPath(value)
    return chain // xPathQuery(value) >> get(1)
end

-- name(node) -> string | nil
-- Function
-- Returns the name of the node.
--
-- Parameters:
--  * node - The node.
--
-- Returns:
--  * The name of the node, or `nil` if it doesn't have one.
local function name(node)
    return node:name()
end

-- attributes(node) -> table | nil
-- Function
-- Returns the attributes of a node as a table if present, or `nil` if not available.
--
-- Parameters:
--  * node - The node.
--
-- Returns:
--  * The attributes table, or `nil`.
local function attributes(node)
    return node:rawAttributes() and node:attributes()
end

-- children(node) -> table
-- Function
-- Returns the children of a node as a table if present, or `{}` if not available.
--
-- Parameters:
--  * node - The node.
--
-- Returns:
--  * The children table, or `nil`.
local function children(node)
    return node:children() or {}
end

-- renderPanel(context) -> none
-- Function<commit_msg>Removed unnecessary code<commit_after>--- === plugins.finalcutpro.toolbox.titlestokeywords ===
---
--- Converts Titles to Keywords

local require                   = require

local hs                        = _G.hs

local log                       = require "hs.logger".new "titlestokeywords"

local dialog                    = require "hs.dialog"
local fs                        = require "hs.fs"
local image                     = require "hs.image"
local inspect                   = require "hs.inspect"

local config                    = require "cp.config"
local fcp                       = require "cp.apple.finalcutpro"
local fcpxml                    = require "cp.apple.fcpxml"
local fn                        = require "cp.fn"
local fntable                   = require "cp.fn.table"
local fnvalue                   = require "cp.fn.value"
local i18n                      = require "cp.i18n"
local time                      = require "cp.apple.fcpxml.time"
local tools                     = require "cp.tools"

local xml                       = require "hs._asm.xml"

local between                   = tools.between
local chain                     = fn.chain
local escapeTilda               = tools.escapeTilda
local get                       = fntable.get
local lines                     = tools.lines
local replace                   = tools.replace
local tableContains             = tools.tableContains
local tableCount                = tools.tableCount
local trim                      = tools.trim
local urlFromPath               = fs.urlFromPath
local webviewAlert              = dialog.webviewAlert
local writeToFile               = tools.writeToFile

local mod = {}

-- ALTERNATIVE_COMMA -> string
-- Constant
-- An alternative comma to use to avoid a FCPXML bug.
local ALTERNATIVE_COMMA = "‚" -- NOTE: This isn't actually a "normal" comma, even though it looks like one in some fonts.

--- plugins.finalcutpro.toolbox.titlestokeywords.addSpaceAfterSequence <cp.prop: boolean>
--- Field
--- Add Space After Sequence
mod.addSpaceAfterSequence = config.prop("toolbox.titlestokeywords.addSpaceAfterSequence", true)

--- plugins.finalcutpro.toolbox.titlestokeywords.treatFavoriteAndRejectAsRatingsInsteadOfKeywords <cp.prop: boolean>
--- Field
--- Treat FAVORITE and REJECT as ratings instead of keywords
mod.treatFavoriteAndRejectAsRatingsInsteadOfKeywords = config.prop("toolbox.titlestokeywords.treatFavoriteAndRejectAsRatingsInsteadOfKeywords", true)

--- plugins.finalcutpro.toolbox.titlestokeywords.replaceCommasWithAlternativeCommas <cp.prop: boolean>
--- Field
--- Remove Project from Event
mod.replaceCommasWithAlternativeCommas = config.prop("toolbox.titlestokeywords.replaceCommasWithAlternativeCommas", true)

--- plugins.finalcutpro.toolbox.titlestokeywords.removeProjectFromEvent <cp.prop: boolean>
--- Field
--- Remove Project from Event
mod.removeProjectFromEvent = config.prop("toolbox.titlestokeywords.removeProjectFromEvent", true)

--- plugins.finalcutpro.toolbox.titlestokeywords.mergeWithExistingEvent <cp.prop: boolean>
--- Field
--- Merge With Existing Event?
mod.mergeWithExistingEvent = config.prop("toolbox.titlestokeywords.mergeWithExistingEvent", false)

--- plugins.finalcutpro.toolbox.titlestokeywords.useTitleContentsInsteadOfTitleName <cp.prop: boolean>
--- Field
--- Use Title Contents Instead of Title Name?
mod.useTitleContentsInsteadOfTitleName = config.prop("toolbox.titlestokeywords.useTitleContentsInsteadOfTitleName", false)

--- plugins.finalcutpro.toolbox.titlestokeywords.textEditor <cp.prop: string>
--- Field
--- Last Text Editor Value
mod.textEditor = config.prop("toolbox.titlestokeywords.textEditor", "")

--- plugins.finalcutpro.toolbox.titlestokeywords.textEditorTwo <cp.prop: string>
--- Field
--- Last Text Editor Two Value
mod.textEditorTwo = config.prop("toolbox.titlestokeywords.textEditorTwo", "")

--- plugins.finalcutpro.toolbox.titlestokeywords.prefix <cp.prop: string>
--- Field
--- Last Prefix Value
mod.prefix = config.prop("toolbox.titlestokeywords.prefix", " - ")

--- plugins.finalcutpro.toolbox.titlestokeywords.suffix <cp.prop: string>
--- Field
--- Last Suffix Value
mod.suffix = config.prop("toolbox.titlestokeywords.suffix", " - ")

--- plugins.finalcutpro.toolbox.titlestokeywords.startOrEnd <cp.prop: string>
--- Field
--- Last Start or End Value
mod.startOrEnd = config.prop("toolbox.titlestokeywords.startOrEnd", "start")

--- plugins.finalcutpro.toolbox.titlestokeywords.startWith <cp.prop: string>
--- Field
--- Last Start With Value
mod.startWith = config.prop("toolbox.titlestokeywords.startWith", "1")

--- plugins.finalcutpro.toolbox.titlestokeywords.duration <cp.prop: string>
--- Field
--- Duration of the titles created by "Create Titles from Text"
mod.duration = config.prop("toolbox.titlestokeywords.duration", "5")

--- plugins.finalcutpro.toolbox.titlestokeywords.stepValue <cp.prop: string>
--- Field
--- Last Step With Value
mod.stepValue = config.prop("toolbox.titlestokeywords.stepValue", "1")

--- plugins.finalcutpro.toolbox.titlestokeywords.padding <cp.prop: string>
--- Field
--- Last Padding Value
mod.padding = config.prop("toolbox.titlestokeywords.padding", "0")

-- xPathQuery(xPath) -> function(node) -> table
-- Function
-- A function combinator that takes an XPath query and returns a function that takes
-- a node and returns the result of the query, usually a table with one or more values.
--
-- Parameters:
--  * xPath - The XPath query.
--
-- Returns:
--  * The function.
local function xPathQuery(xPath)
    return function(node)
        return node:XPathQuery(xPath)
    end
end

-- xPath(value) -> function(node) -> anything | nil
-- Function
-- A function combinator that takes an XPath query and returns a chain function that takes
-- a node and returns the first result of the query, or `nil` if nothing was found.
--
-- Parameters:
--  * xPath - The XPath query.
--
-- Returns:
--  * The chain function.
local function xPath(value)
    return chain // xPathQuery(value) >> get(1)
end

-- children(node) -> table
-- Function
-- Returns the children of a node as a table if present, or `{}` if not available.
--
-- Parameters:
--  * node - The node.
--
-- Returns:
--  * The children table, or `nil`.
local function children(node)
    return node:children() or {}
end

-- renderPanel(context) -> none
-- Function
{'fraction_non_alphanumeric': 0.12131586781780292, 'fraction_numerical': 0.0013396844298898482, 'mean_word_length': 2.7325, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1369138', 'n_tokens_mistral': 3928, 'n_tokens_neox': 3748, 'n_words': 998}