From 0bb74650d259304f9fc1179530bc41dd85d8090b Mon Sep 17 00:00:00 2001 From: Bastien Dumont Date: Sun, 24 Oct 2021 18:45:37 +0200 Subject: [PATCH] =?UTF-8?q?Enum=C3=A9ration=20fonctionnelle=20pour=20opend?= =?UTF-8?q?ocument=20et=20openxml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- text-crossrefs.lua | 60 +++++++++++++++++++++++++++++++++++++++------- 2 files changed, 52 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 4ac69b6..c06f5ea 100644 --- a/README.md +++ b/README.md @@ -107,7 +107,7 @@ The following metadata fields can be set as strings: * the string inserted between to page numbers in a range; * defaults to `-`. * `tcrf-references-enum-separator`: - * the string used to separate the elements of an enumeration in a reference span; can be composed of any character not authorized in an identifier other than space or tab; + * the character used to separate the elements of an enumeration in a reference span; can be any character not authorized in an identifier other than space or tab; * defaults to `;`. * `tcrf-multiple-delimiter`: * the string inserted between two elements (but the two last ones) in an enumeration; diff --git a/text-crossrefs.lua b/text-crossrefs.lua index cb708ca..167cdfd 100644 --- a/text-crossrefs.lua +++ b/text-crossrefs.lua @@ -60,6 +60,7 @@ local config = { multiple_before_last = ' and ', references_range_separator = '>', range_separator = '-', + references_enum_separator = ';', only_explicit_labels = 'false', default_info_type = 'page', filelabel_ref_separator = '::' @@ -337,12 +338,21 @@ local function get_second_reference(rawref) if is_reference_valid(ref) then return ref end end +local function is_ref_enumeration(raw_reference) + if string.match(raw_reference, '%' .. config.references_enum_separator) then + return true + else + return false + end +end + local function analyze_reference_span(reference_span) if #reference_span.content == 1 and reference_span.content[1].t == 'Str' then raw_reference = reference_span.content[1].c analyzed_reference = {} analyzed_reference.is_external = is_ref_external(raw_reference) analyzed_reference.is_range = is_ref_range(raw_reference) + analyzed_reference.is_enumeration = is_ref_enumeration(raw_reference) if analyzed_reference.is_external then analyzed_reference.filelabel = get_extfilelabel(raw_reference) end @@ -442,7 +452,7 @@ local function format_pagenote_reference(target) end local function format_reference(target, info_type) - if info_type == 'page' and target.is_range then + if info_type == 'page' and target.is_range then return format_pagerange_reference(target.first, target.second, not target.is_enumeration) elseif info_type == 'page' then @@ -457,20 +467,53 @@ local function format_reference(target, info_type) end end -local function make_reference_head(info_type, is_enumeration) - if info_type == 'page' or info_type == 'note' then +local function make_reference_head(info_type, is_enumeration, is_range) + if (info_type == 'page' or info_type == 'note') and not is_range then return format_prefix(info_type, is_enumeration) else return '' end end -local function make_reference_body(target, info_type) - +local function format_all_items_in_enumeration(enum, info_type) + local reference_body = '' + local enumerated_ref_spans = {} + for ref in string.gmatch(enum, + '[^%' .. config.references_enum_separator .. ']+') do + ref_span = pandoc.Span(ref, {['type'] = info_type}) + table.insert(enumerated_ref_spans, ref_span) + end + for i = 1, #enumerated_ref_spans do + target_in_enum = analyze_reference_span(enumerated_ref_spans[i]) + reference_body = reference_body .. format_reference(target_in_enum, info_type) + if i < #enumerated_ref_spans then + if i < #enumerated_ref_spans-1 then + reference_body = reference_body .. config.multiple_delimiter + else + reference_body = reference_body .. config.multiple_before_last + end + end + end + return reference_body end -local function make_reference_tail(info_type) +local function format_enumeration(target, info_type) + if RAW_ATTRIBUTE == 'context' or RAW_ATTRIBUTE == 'latex' then + -- TODO + return format_enumeration_smart(target.first, info_type) + elseif RAW_ATTRIBUTE == 'opendocument' or RAW_ATTRIBUTE == 'openxml' then + return format_all_items_in_enumeration(target.first, info_type) + end +end +local function make_reference_body(target, info_type) + local reference_body + if target.is_enumeration then + reference_body = format_enumeration(target, info_type) + else + reference_body = format_reference(target, info_type) + end + return reference_body end local function make_reference(span) @@ -478,10 +521,9 @@ local function make_reference(span) local target = analyze_reference_span(span) if not target.is_external then local info_type = span.attributes.type or config.default_info_type - local head = make_reference_head(info_type, target.is_enumeration) + local head = make_reference_head(info_type, target.is_enumeration, target.is_range) local body = make_reference_body(target, info_type) - local tail = make_reference_tail(info_type) - span.content[1] = pandoc.RawInline(RAW_ATTRIBUTE, head .. body .. tail) + span.content[1] = pandoc.RawInline(RAW_ATTRIBUTE, head .. body) return span end end