KiCad PCB EDA Suite
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 <pavlina.chris@gmail.com>
5  * Copyright (C) 2017 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 
28 static const wxString DescriptionFormat =
29  "<b>__NAME__</b>"
30  "<br>__DESC__"
31  "<hr><table border=0>"
32  "__FIELDS__"
33  "</table>";
34 
35 static const wxString KeywordsFormat =
36  "<tr>"
37  " <td><b>" + _( "Keywords" ) + "</b></td>"
38  " <td>__KEYWORDS__</td>"
39  "</tr>";
40 
41 static const wxString DocFormat =
42  "<tr>"
43  " <td><b>" + _( "Documentation" ) + "</b></td>"
44  " <td><a href=\"__HREF__\">__TEXT__</a></td>"
45  "</tr>";
46 
47 
49 {
50  wxString m_html;
52  LIB_ID const m_lib_id;
53 
55 
56 public:
57  FOOTPRINT_INFO_GENERATOR( FP_LIB_TABLE* aFpLibTable, LIB_ID const& aLibId )
59  m_fp_lib_table( aFpLibTable ),
60  m_lib_id( aLibId ),
61  m_footprint( nullptr )
62  { }
63 
67  void GenerateHtml()
68  {
69  wxCHECK_RET( m_fp_lib_table, "Footprint library table pointer is not valid" );
70 
71  if( !m_lib_id.IsValid() )
72  return;
73 
74  try
75  {
78  }
79  catch( const IO_ERROR& ioe )
80  {
81  wxLogError( _( "Error loading footprint %s from library '%s'." ) + wxS( "\n%s" ),
84  ioe.What() );
85  return;
86  }
87 
88  if( m_footprint )
89  {
90  wxString name = m_lib_id.GetLibItemName();
91  wxString desc = m_footprint->GetDescription();
92  wxString keywords = m_footprint->GetKeywords();
93  wxString doc;
94 
95  // It is currently common practice to store a documentation link in the description.
96  int idx = desc.find( wxT( "http:" ) );
97 
98  if( idx < 0 )
99  idx = desc.find( wxT( "https:" ) );
100 
101  if( idx >= 0 )
102  {
103  // And, sadly, it appears to have also become customary to bury the url inside
104  // parentheses.
105  if( idx >= 1 && desc.at( idx - 1 ) == '(' )
106  {
107  int nesting = 0;
108 
109  while( idx < (int) desc.size() )
110  {
111  char c = desc.at( idx++ );
112 
113  if( c == '(' )
114  nesting++;
115  else if( c == ')' && --nesting < 0 )
116  break;
117 
118  doc += c;
119  }
120 
121  desc.Replace( doc, _( "doc url" ) );
122  }
123  else
124  {
125  doc = desc.substr( (unsigned) idx );
126 
127  desc = desc.substr( 0, (unsigned) idx );
128  desc = desc.Trim( true );
129 
130  if( !desc.IsEmpty() && desc.Last() == ',' )
131  desc.RemoveLast( 1 );
132  }
133  }
134 
135  m_html.Replace( "__NAME__", EscapeHTML( name ) );
136  m_html.Replace( "__DESC__", EscapeHTML( desc ) );
137 
138  wxString keywordsHtml = KeywordsFormat;
139  keywordsHtml.Replace( "__KEYWORDS__", EscapeHTML( keywords ) );
140 
141  wxString docHtml = DocFormat;
142  docHtml.Replace( "__HREF__", EscapeHTML( doc ) );
143 
144  if( doc.Length() > 75 )
145  doc = doc.Left( 72 ) + wxT( "..." );
146 
147  docHtml.Replace( "__TEXT__", EscapeHTML( doc ) );
148 
149  m_html.Replace( "__FIELDS__", keywordsHtml + docHtml );
150  }
151  }
152 
156  wxString GetHtml()
157  {
158  return m_html;
159  }
160 
161 };
162 
163 
164 wxString GenerateFootprintInfo( FP_LIB_TABLE* aFpLibTable, LIB_ID const& aLibId )
165 {
166  FOOTPRINT_INFO_GENERATOR gen( aFpLibTable, aLibId );
167  gen.GenerateHtml();
168  return gen.GetHtml();
169 }
const wxString & GetDescription() const
Definition: footprint.h:197
const UTF8 & GetLibItemName() const
Definition: lib_id.h:104
void GenerateHtml()
Generate the HTML internally.
static const wxString KeywordsFormat
static const wxString DocFormat
wxString GenerateFootprintInfo(FP_LIB_TABLE *aFpLibTable, LIB_ID const &aLibId)
Return an HTML page describing a LIB_ID in a FP_LIB_TABLE.
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
bool IsValid() const
Check if this LID_ID is valid.
Definition: lib_id.h:159
#define gen(mvar, chg_bit, get, set, add, sub, changed)
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
FOOTPRINT_INFO_GENERATOR(FP_LIB_TABLE *aFpLibTable, LIB_ID const &aLibId)
#define _(s)
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:90
const wxString & GetKeywords() const
Definition: footprint.h:200
wxString GetHtml()
Return the generated HTML.
const FOOTPRINT * GetEnumeratedFootprint(const wxString &aNickname, const wxString &aFootprintName)
A version of FootprintLoad() for use after FootprintEnumerate() for more efficient cache management.
wxString EscapeHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
const char * name
Definition: DXF_plotter.cpp:56
wxString wx_str() const
Definition: utf8.cpp:46
static const wxString DescriptionFormat
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75