New filter `urlify` to convert URLs in fields to HTML links

Closes #311
This commit is contained in:
Salvador E. Tropea 2022-09-30 11:28:21 -03:00
parent 193632ff65
commit a9d5b8cd58
9 changed files with 69 additions and 4 deletions

View File

@ -44,6 +44,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Internal BoM: Now you can aggregate components using CSV files. (See #248)
- Now you can check PCB and schematic parity using the `update_xml` preflight
(See #297)
- New filter `urlify` to convert URLs in fields to HTML links (#311)
### Fixed
- Problems to compress netlists. (#287)

View File

@ -893,6 +893,12 @@ filters:
- `split_fields_expand`: [boolean=false] When `true` the fields in `split_fields` are added to the internal names.
- `use_ref_sep_for_first`: [boolean=true] Force the reference separator use even for the first component in the list (KiCost behavior).
- `value_alt_field`: [string='value_subparts'] Field containing replacements for the `Value` field. So we get real values for split parts.
- urlify: URLify
Converts URL text in fields to HTML URLs.
* Valid keys:
- `comment`: [string=''] A comment for documentation purposes.
- `fields`: [string|list(string)='Datasheet'] Fields to convert.
- `name`: [string=''] Used to identify this particular filter definition.
- var_rename: Var_Rename
This filter implements the VARIANT:FIELD=VALUE renamer to get FIELD=VALUE when VARIANT is in use.
* Valid keys:
@ -930,6 +936,7 @@ The [tests/yaml_samples](https://github.com/INTI-CMNB/KiBot/tree/master/tests/ya
#### Built-in filters
- **_datasheet_link** converts Datasheet fields containing URLs into HTML links
- **_expand_text_vars** is a default `expand_text_vars` filter
- **_kibom_dnc_Config** it uses the internal `dnc_list` to exclude components with
- Value matching any of the keys

View File

@ -521,6 +521,7 @@ The [tests/yaml_samples](https://github.com/INTI-CMNB/KiBot/tree/master/tests/ya
#### Built-in filters
- **_datasheet_link** converts Datasheet fields containing URLs into HTML links
- **_expand_text_vars** is a default `expand_text_vars` filter
- **_kibom_dnc_Config** it uses the internal `dnc_list` to exclude components with
- Value matching any of the keys

View File

@ -7,7 +7,7 @@ from .registrable import RegFilter, Registrable, RegOutput
from .optionable import Optionable
from .gs import GS
from .misc import (IFILT_MECHANICAL, IFILT_VAR_RENAME, IFILT_ROT_FOOTPRINT, IFILT_KICOST_RENAME, DISTRIBUTORS,
IFILT_VAR_RENAME_KICOST, IFILT_KICOST_DNP, IFILT_EXPAND_TEXT_VARS)
IFILT_VAR_RENAME_KICOST, IFILT_KICOST_DNP, IFILT_EXPAND_TEXT_VARS, IFILT_DATASHEET_LINK)
from .error import KiPlotConfigurationError
from .bom.columnlist import ColumnList
from .macros import macros, document # noqa: F401
@ -251,6 +251,14 @@ class BaseFilter(RegFilter):
logger.debug('Creating internal filter: '+str(o_tree))
return o_tree
@staticmethod
def _create_datasheet_link(name):
o_tree = {'name': name}
o_tree['type'] = 'urlify'
o_tree['comment'] = 'Internal datasheet URL to HTML link'
logger.debug('Creating internal filter: '+str(o_tree))
return o_tree
@staticmethod
def _create_kibom_dnx(name):
type = name[7:10]
@ -318,6 +326,8 @@ class BaseFilter(RegFilter):
tree = BaseFilter._create_kicost_dnp(name)
elif name == IFILT_EXPAND_TEXT_VARS:
tree = BaseFilter._create_expand_text_vars(name)
elif name == IFILT_DATASHEET_LINK:
tree = BaseFilter._create_datasheet_link(name)
else:
return None
filter = RegFilter.get_class_for(tree['type'])()

44
kibot/fil_urlify.py Normal file
View File

@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2022 Salvador E. Tropea
# Copyright (c) 2022 Instituto Nacional de Tecnología Industrial
# License: GPL-3.0
# Project: KiBot (formerly KiPlot)
# Description: Converts URL style text into HTML links
import re
from .optionable import Optionable
from .macros import macros, document, filter_class # noqa: F401
from . import log
logger = log.get_logger()
regex = re.compile(r'(https?://\S+)')
@filter_class
class URLify(BaseFilter): # noqa: F821
""" URLify
Converts URL text in fields to HTML URLs """
def __init__(self):
super().__init__()
self._is_transform = True
with document:
self.fields = Optionable
""" [string|list(string)='Datasheet'] Fields to convert """
self._fields_example = 'Datasheet'
def config(self, parent):
super().config(parent)
if isinstance(self.fields, type):
self.fields = ['Datasheet']
self.fields = Optionable.force_list(self.fields)
def filter(self, comp):
""" Look for URLs and convert them """
for fld in self.fields:
value = comp.get_field_value(fld)
if not value:
continue
new_value = regex.sub(r'<a href="\1">\1</a>', value)
if new_value != value:
logger.debugl(2, '{}: {} -> {}'.format(fld, value, new_value))
comp.set_field(fld, new_value)

View File

@ -1923,7 +1923,7 @@ class Schematic(object):
SubElement(comp, 'value').text = c.value
if c.footprint:
SubElement(comp, 'footprint').text = c.footprint
if len(c.datasheet) and not (self.netlist_version == 'E' and c.datasheet != '~'):
if len(c.datasheet) and not (self.netlist_version == 'E' and c.datasheet == '~'):
SubElement(comp, 'datasheet').text = c.datasheet
user_fields = c.get_user_fields()
if user_fields:

View File

@ -89,6 +89,7 @@ IFILT_ROT_FOOTPRINT = '_rot_footprint'
IFILT_KICOST_RENAME = '_kicost_rename'
IFILT_KICOST_DNP = '_kicost_dnp'
IFILT_EXPAND_TEXT_VARS = '_expand_text_vars'
IFILT_DATASHEET_LINK = '_datasheet_link'
# KiCad 5 GUI values for the attribute
UI_THT = 0 # 1 for KiCad 6
UI_SMD = 1 # 2 for KiCad 6

View File

@ -77,7 +77,7 @@
(property "Footprint" "Resistor_SMD:R_0805_2012Metric" (id 2) (at 54.102 64.77 90)
(effects (font (size 1.27 1.27)) hide)
)
(property "Datasheet" "~" (id 3) (at 55.88 64.77 0)
(property "Datasheet" "https://www.vishay.com/docs/28705/mcx0x0xpro.pdf" (id 3) (at 55.88 64.77 0)
(effects (font (size 1.27 1.27)) hide)
)
(pin "1" (uuid 49c4dcb9-cb9b-421e-b7b9-e495c75a42b7))

View File

@ -2,11 +2,11 @@
kibot:
version: 1
variants:
- name: 'default'
comment: 'Default KiBoM variant'
type: kibom
pre_transform: _datasheet_link
- name: 't1_v1'
comment: 'Test 1 Variant V1'
@ -39,6 +39,7 @@ outputs:
dir: BoM
options:
variant: 'default'
extra_fields: Datasheet
- name: 'bom_internal_v1'
comment: "Bill of Materials in CSV format for variant t1_v1"