Research Notes Report with Options.fh_lua

--[[
@Title: Research Notes Report with Options
@Type: Report
@Author: ColeValleyGirl
@Contributors: Calico Pie
@Version: 1.0
@Keywords: Research
@LastUpdated: 19 January 2023
@Licence: This plugin is copyright (c) 2023 Helen Wright & contributors and is licensed under the MIT License
which is hereby incorporated by reference (see https://pluginstore.family-historian.co.uk/fh-plugin-licence)
@Description: Report on a selection of Research Notes. It uses a Title field to generate the record Header. If no Title label is specified, it defaults to "Title".
Options are provided to: Specify the field to be used as a title, include the record ID in the header, and remove the header text from the body of the note to avoid duplication.
]]

fhSetStringEncoding "UTF-8" --Use UTF-8 as current string encoding.
utf8 = require(".utf8"):init() --enable UTF8 string handling
require "pl.init" -- Penlight library

-- Constants available in all functions.
local sReportOptsFile = fhGetContextInfo "CI_REPORT_OPTIONS_FILE"
local defaultTitle = "Title"

--Variables available to all functions
local report_settings = {} -- Table for Report settings used in the plugin

------------------------------------------------------------
-- FH_Requirements
------------------------------------------------------------
function FH_Requirements()
	local t = {}

	-- default startup options
	t.record_type = "_RNOT"
	t.generations = false

	-- report options dialog requirements
	t.title = "Research Notes"
	t.tabs = "sources, format" -- report options tabs required (may not show in rpt opts when report is part of a book)
	t.fonts = "hdg1,secdata" -- fonts shown in format tab of report options
	t.heading_levels = 1 -- affects no. of heading levels shown in format tab of report options
	t.column_indents = 0 -- no. of column indent value required in layout tab of rpt opts
	t.para_indent = false -- para indent value required in layout tab of rpt opts
	t.options = -- Heading and Body options
		"~titleField|Field to use as Title|edit|" .. defaultTitle .. "|110:200 \n" .. "~includeID|Include record ID in heading|checkbox|false|340 \n" .. "~removeTitle|Remove title from body|checkbox|true|340 \n"
	return t
end
------------------------------------------------------------
-- FH_Reset
------------------------------------------------------------
function FH_Reset(sTab)
	if not sTab or sTab == "Main" then
		fhSetIniFileValue(
			sReportOptsFile,
			"Plugin",
			"titleField",
			"text",
			defaultTitle
		)
		fhSetIniFileValue(sReportOptsFile, "Plugin", "includeID", "bool", false)
		fhSetIniFileValue(sReportOptsFile, "Plugin", "removeTitle", "bool", true)
		return true
	end
	return false
end
---------------------------------------------------------------------------
-- getSettings
---------------------------------------------------------------------------
local function getSettings()
	report_settings.titleField = fhGetIniFileValue(
		sReportOptsFile,
		"Plugin",
		"titleField",
		"text",
		defaultTitle
	)
	report_settings.includeID =
		fhGetIniFileValue(sReportOptsFile, "Plugin", "includeID", "bool", false)
	report_settings.removeTitle =
		fhGetIniFileValue(sReportOptsFile, "Plugin", "removeTitle", "bool", true)
end
------------------------------------------------------------
-- FH_GetRecordSectionContent
------------------------------------------------------------
function FH_GetRecordSectionContent(snTop, rec, index, count)
	getSettings()

	local titleLabel = utils.choose(
		utf8.len(stringx.strip(report_settings.titleField)) > 0,
		stringx.strip(report_settings.titleField),
		defaultTitle
	)
	local emptyTitle = "(No " .. titleLabel .. ")"
	local emptyNote = "(Empty note)"

	local pr = fhNewItemPtr()
	pr:MoveTo(rec, "~.TEXT")
	local rtHeading = fhNewRichText() --report item heading
	local rtBody = fhNewRichText() --report item body
	------------------------------------------------------- prefix heading with Record ID if required
	if report_settings.includeID then
		rtHeading:SetText("[" .. tostring(fhGetRecordId(rec)) .. "] ", false)
	end
	-------------------------------------------------------- find title
	local rtEFTFBody = fhGetValueAsRichText(pr):GetText() --get an eFTF version of the note
	if pr:IsNull() or utf8.len(stringx.strip(rtEFTFBody)) == 0 then
		rtHeading:AddText(emptyTitle, false)
		rtBody:SetText(emptyNote, false)
	else
		local titleText, existsTitle --used for found header text
		titleText, existsTitle = fhGetLabelledText(pr, titleLabel .. ":") --get labelled text
		if utf8.len(stringx.strip(titleText)) == 0 then -- labelled text is not blank
			titleText = emptyTitle
		end
		rtHeading:AddText(stringx.strip(titleText), true) --remove leading and trailing spaces and set header
		--------------------------------------------------------- Remove titletxt from body if requested
		if report_settings.removeTitle and existsTitle then
			rtEFTFBody = string.gsub(
				rtEFTFBody,
				"(%s-)" .. titleLabel .. ":%s-" .. titleText .. "%s-\n",
				"%1",
				1
			) --remove the label and all text to the end of the paragraph including the new line
		end
		rtBody:SetText(rtEFTFBody, true, false)
	end
	snTop:SetHeading(rtHeading)
	snTop:SetBodyText(rtBody)

	-- Append all Text Level and Record Level Sources to end of report
	rtBody:AddCitation(pr, false)
	rtBody:AddCitation(rec, false)
end

------------------------------------------------------------
-- Main
------------------------------------------------------------
if DEBUG_MODE then
	local sn = fhNewSection()
	local pr = fhNewItemPtr()
	pr:MoveToFirstRecord "_RNOT"
	while not pr:IsNull() do
		FH_GetRecordSectionContent(sn, pr, 1, 1)
		pr:MoveNext()
	end
end

Source:Research-Notes-Report-with-Options.fh_lua