KiCad PCB EDA Suite
Loading...
Searching...
No Matches
generate_footprint_info.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2017 Chris Pavlina <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
22#include <ki_exception.h>
23#include <string_utils.h>
24#include <footprint.h>
26#include <trace_helpers.h>
27#include <wx/log.h>
28
29
30static const wxString DescriptionFormat = wxT(
31 "<b>__NAME__</b>"
32 "<br>__DESC__"
33 "<hr><table border=0>"
34 "__FIELDS__"
35 "</table>" );
36
37static const wxString KeywordsFormat = wxT(
38 "<tr>"
39 " <td><b>" + _( "Keywords" ) + "</b></td>"
40 " <td>__KEYWORDS__</td>"
41 "</tr>" );
42
43static const wxString DocFormat = wxT(
44 "<tr>"
45 " <td><b>" + _( "Documentation" ) + "</b></td>"
46 " <td><a href=\"__HREF__\">__TEXT__</a></td>"
47 "</tr>" );
48
49
50std::optional<wxString> GetFootprintDocumentationURL( const FOOTPRINT& aFootprint )
51{
52 // Footprints have now a field (FIELD_T::DATASHEET) containing the url datasheet
53 // But old footprints did not have this field, so this fiels can be empty.
54 // So we use this field is not empty, and if empty see if the documentation has an URL
55 const PCB_FIELD* data_field = aFootprint.GetField( FIELD_T::DATASHEET );
56 wxString url = data_field->GetText();
57
58 if( !url.IsEmpty() )
59 return url;
60
61 wxString desc = aFootprint.GetLibDescription();
62
63 // It is (or was) currently common practice to store a documentation link in the description.
64 size_t idx = desc.find( wxT( "http:" ) );
65
66 if( idx == wxString::npos )
67 idx = desc.find( wxT( "https:" ) );
68
69 if( idx == wxString::npos )
70 return std::nullopt;
71
72 int nesting = 0;
73
74 for( auto chit = desc.begin() + idx; chit != desc.end(); ++chit )
75 {
76 int ch = *chit;
77
78 // Break on invalid URI characters
79 if( ch <= 0x20 || ch >= 0x7F || ch == '"' )
80 break;
81
82 // Check for nesting parentheses, e.g. (Body style from: https://this.url/part.pdf)
83 if( ch == '(' )
84 ++nesting;
85 else if( ch == ')' && --nesting < 0 )
86 break;
87
88 url += ch;
89 }
90
91 // Trim trailing punctuation
92 static wxString punct = wxS( ".,:;" );
93
94 if( punct.find( url.Last() ) != wxString::npos )
95 url = url.Left( url.Length() - 1 );
96
97 if( url.IsEmpty() )
98 return std::nullopt;
99
100 return url;
101}
102
103
105{
106public:
109 m_adapter( aAdapter ),
110 m_lib_id( aLibId ),
111 m_footprint( nullptr )
112 { }
113
118 {
119 wxCHECK_RET( m_adapter, wxT( "Footprint library table pointer is not valid" ) );
120
121 if( !m_lib_id.IsValid() )
122 return;
123
124 try
125 {
126 m_footprint = m_adapter->LoadFootprint( m_lib_id, false );
127 }
128 catch( const IO_ERROR& ioe )
129 {
130 // Log to trace instead of error to avoid popup dialogs
131 wxLogTrace( traceLibraries, "Error loading footprint %s from library '%s': %s",
132 m_lib_id.GetLibItemName().wx_str(),
133 m_lib_id.GetLibNickname().wx_str(),
134 ioe.What() );
135 return;
136 }
137
138 if( m_footprint )
139 {
140 wxString name = m_lib_id.GetLibItemName();
141 wxString desc = m_footprint->GetLibDescription();
142 wxString keywords = m_footprint->GetKeywords();
143 wxString doc;
144
145 if( std::optional<wxString> url = GetFootprintDocumentationURL( *m_footprint ) )
146 doc = *url;
147
148 wxString esc_desc = EscapeHTML( UnescapeString( desc ) );
149
150 // Add line breaks
151 esc_desc.Replace( wxS( "\n" ), wxS( "<br>" ) );
152
153 // Add links
154 esc_desc = LinkifyHTML( esc_desc );
155
156 m_html.Replace( "__DESC__", esc_desc );
157 m_html.Replace( "__NAME__", EscapeHTML( name ) );
158
159 wxString keywordsHtml = KeywordsFormat;
160 keywordsHtml.Replace( "__KEYWORDS__", EscapeHTML( keywords ) );
161
162 wxString docHtml = DocFormat;
163 docHtml.Replace( "__HREF__", EscapeHTML( doc ) );
164
165 if( doc.Length() > 75 )
166 doc = doc.Left( 72 ) + wxT( "..." );
167
168 docHtml.Replace( "__TEXT__", EscapeHTML( doc ) );
169
170 m_html.Replace( "__FIELDS__", keywordsHtml + docHtml );
171 }
172 }
173
177 wxString GetHtml()
178 {
179 return m_html;
180 }
181
182private:
183 wxString m_html;
185 LIB_ID const m_lib_id;
186
188};
189
190
191wxString GenerateFootprintInfo( FOOTPRINT_LIBRARY_ADAPTER* aAdapter, LIB_ID const& aLibId )
192{
193 FOOTPRINT_INFO_GENERATOR gen( aAdapter, aLibId );
194 gen.GenerateHtml();
195 return gen.GetHtml();
196}
const char * name
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition eda_text.h:98
void GenerateHtml()
Generate the HTML internally.
FOOTPRINT_LIBRARY_ADAPTER * m_adapter
FOOTPRINT_INFO_GENERATOR(FOOTPRINT_LIBRARY_ADAPTER *aAdapter, LIB_ID const &aLibId)
wxString GetHtml() const
Return the generated HTML.
wxString GetHtml()
Return the generated HTML.
An interface to the global shared library manager that is schematic-specific and linked to one projec...
wxString GetLibDescription() const
Definition footprint.h:358
PCB_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this footprint.
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:49
#define _(s)
static const wxString KeywordsFormat
static const wxString DescriptionFormat
static const wxString KeywordsFormat
static const wxString DescriptionFormat
wxString GenerateFootprintInfo(FOOTPRINT_LIBRARY_ADAPTER *aAdapter, LIB_ID const &aLibId)
Return an HTML page describing a LIB_ID in a footprint library.
static const wxString DocFormat
std::optional< wxString > GetFootprintDocumentationURL(const FOOTPRINT &aFootprint)
Get a URL to the documentation for a LIB_ID in a #FP_LIB_TABLE.
const wxChar *const traceLibraries
Flag to enable library table and library manager tracing.
wxString EscapeHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
wxString UnescapeString(const wxString &aSource)
wxString LinkifyHTML(wxString aStr)
Wraps links in HTML tags.
@ DATASHEET
name of datasheet
wxLogTrace helper definitions.