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 <string_utils.h>
23#include <footprint.h>
24#include <fp_lib_table.h>
25#include <wx/log.h>
26
27
28static const wxString DescriptionFormat = wxT(
29 "<b>__NAME__</b>"
30 "<br>__DESC__"
31 "<hr><table border=0>"
32 "__FIELDS__"
33 "</table>" );
34
35static const wxString KeywordsFormat = wxT(
36 "<tr>"
37 " <td><b>" + _( "Keywords" ) + "</b></td>"
38 " <td>__KEYWORDS__</td>"
39 "</tr>" );
40
41static const wxString DocFormat = wxT(
42 "<tr>"
43 " <td><b>" + _( "Documentation" ) + "</b></td>"
44 " <td><a href=\"__HREF__\">__TEXT__</a></td>"
45 "</tr>" );
46
47
48std::optional<wxString> GetFootprintDocumentationURL( const FOOTPRINT& aFootprint )
49{
50 // Footprints have now a field (FIELD_T::DATASHEET) containing the url datasheet
51 // But old footprints did not have this field, so this fiels can be empty.
52 // So we use this field is not empty, and if empty see if the documentation has an URL
53 const PCB_FIELD* data_field = aFootprint.GetField( FIELD_T::DATASHEET );
54 wxString url = data_field->GetText();
55
56 if( !url.IsEmpty() )
57 return url;
58
59 wxString desc = aFootprint.GetLibDescription();
60
61 // It is (or was) currently common practice to store a documentation link in the description.
62 size_t idx = desc.find( wxT( "http:" ) );
63
64 if( idx == wxString::npos )
65 idx = desc.find( wxT( "https:" ) );
66
67 if( idx == wxString::npos )
68 return std::nullopt;
69
70 int nesting = 0;
71
72 for( auto chit = desc.begin() + idx; chit != desc.end(); ++chit )
73 {
74 int ch = *chit;
75
76 // Break on invalid URI characters
77 if( ch <= 0x20 || ch >= 0x7F || ch == '"' )
78 break;
79
80 // Check for nesting parentheses, e.g. (Body style from: https://this.url/part.pdf)
81 if( ch == '(' )
82 ++nesting;
83 else if( ch == ')' && --nesting < 0 )
84 break;
85
86 url += ch;
87 }
88
89 // Trim trailing punctuation
90 static wxString punct = wxS( ".,:;" );
91
92 if( punct.find( url.Last() ) != wxString::npos )
93 url = url.Left( url.Length() - 1 );
94
95 if( url.IsEmpty() )
96 return std::nullopt;
97
98 return url;
99}
100
101
103{
104public:
105 FOOTPRINT_INFO_GENERATOR( FP_LIB_TABLE* aFpLibTable, LIB_ID const& aLibId ) :
107 m_fp_lib_table( aFpLibTable ),
108 m_lib_id( aLibId ),
109 m_footprint( nullptr )
110 { }
111
116 {
117 wxCHECK_RET( m_fp_lib_table, wxT( "Footprint library table pointer is not valid" ) );
118
119 if( !m_lib_id.IsValid() )
120 return;
121
122 try
123 {
126 }
127 catch( const IO_ERROR& ioe )
128 {
129 wxLogError( _( "Error loading footprint %s from library '%s'." ) + wxS( "\n%s" ),
132 ioe.What() );
133 return;
134 }
135
136 if( m_footprint )
137 {
138 wxString name = m_lib_id.GetLibItemName();
139 wxString desc = m_footprint->GetLibDescription();
140 wxString keywords = m_footprint->GetKeywords();
141 wxString doc;
142
143 if( std::optional<wxString> url = GetFootprintDocumentationURL( *m_footprint ) )
144 doc = *url;
145
146 wxString esc_desc = EscapeHTML( UnescapeString( desc ) );
147
148 // Add line breaks
149 esc_desc.Replace( wxS( "\n" ), wxS( "<br>" ) );
150
151 // Add links
152 esc_desc = LinkifyHTML( esc_desc );
153
154 m_html.Replace( "__DESC__", esc_desc );
155 m_html.Replace( "__NAME__", EscapeHTML( name ) );
156
157 wxString keywordsHtml = KeywordsFormat;
158 keywordsHtml.Replace( "__KEYWORDS__", EscapeHTML( keywords ) );
159
160 wxString docHtml = DocFormat;
161 docHtml.Replace( "__HREF__", EscapeHTML( doc ) );
162
163 if( doc.Length() > 75 )
164 doc = doc.Left( 72 ) + wxT( "..." );
165
166 docHtml.Replace( "__TEXT__", EscapeHTML( doc ) );
167
168 m_html.Replace( "__FIELDS__", keywordsHtml + docHtml );
169 }
170 }
171
175 wxString GetHtml()
176 {
177 return m_html;
178 }
179
180private:
181 wxString m_html;
183 LIB_ID const m_lib_id;
184
186};
187
188
189wxString GenerateFootprintInfo( FP_LIB_TABLE* aFpLibTable, LIB_ID const& aLibId )
190{
191 FOOTPRINT_INFO_GENERATOR gen( aFpLibTable, aLibId );
192 gen.GenerateHtml();
193 return gen.GetHtml();
194}
const char * name
Definition: DXF_plotter.cpp:62
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:97
FOOTPRINT_INFO_GENERATOR(FP_LIB_TABLE *aFpLibTable, LIB_ID const &aLibId)
void GenerateHtml()
Generate the HTML internally.
wxString GetHtml() const
Return the generated HTML.
wxString GetHtml()
Return the generated HTML.
wxString GetLibDescription() const
Definition: footprint.h:260
PCB_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this footprint.
Definition: footprint.cpp:593
wxString GetKeywords() const
Definition: footprint.h:263
const FOOTPRINT * GetEnumeratedFootprint(const wxString &aNickname, const wxString &aFootprintName)
A version of FootprintLoad() for use after FootprintEnumerate() for more efficient cache management.
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
bool IsValid() const
Check if this LID_ID is valid.
Definition: lib_id.h:172
const UTF8 & GetLibItemName() const
Definition: lib_id.h:102
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:87
wxString wx_str() const
Definition: utf8.cpp:45
#define _(s)
static const wxString KeywordsFormat
wxString GenerateFootprintInfo(FP_LIB_TABLE *aFpLibTable, LIB_ID const &aLibId)
Return an HTML page describing a LIB_ID in a FP_LIB_TABLE.
static const wxString DescriptionFormat
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.
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