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 <kicad_string.h>
23 #include <footprint.h>
24 #include <fp_lib_table.h>
25 
26 
27 static const wxString DescriptionFormat =
28  "<b>__NAME__</b>"
29  "<br>__DESC__"
30  "<hr><table border=0>"
31  "__FIELDS__"
32  "</table>";
33 
34 static const wxString KeywordsFormat =
35  "<tr>"
36  " <td><b>" + _( "Keywords" ) + "</b></td>"
37  " <td>__KEYWORDS__</td>"
38  "</tr>";
39 
40 static const wxString DocFormat =
41  "<tr>"
42  " <td><b>" + _( "Documentation" ) + "</b></td>"
43  " <td><a href=\"__HREF__\">__TEXT__</a></td>"
44  "</tr>";
45 
46 
48 {
49  wxString m_html;
51  LIB_ID const m_lib_id;
52 
54 
55 public:
56  FOOTPRINT_INFO_GENERATOR( FP_LIB_TABLE* aFpLibTable, LIB_ID const& aLibId )
58  m_fp_lib_table( aFpLibTable ),
59  m_lib_id( aLibId ),
60  m_footprint( nullptr )
61  { }
62 
66  void GenerateHtml()
67  {
68  wxCHECK_RET( m_fp_lib_table, "Footprint library table pointer is not valid" );
69 
70  if( !m_lib_id.IsValid() )
71  return;
72 
73  try
74  {
77  }
78  catch( const IO_ERROR& ioe )
79  {
80  wxLogError( wxString::Format( _( "Error loading footprint %s from library %s.\n\n%s" ),
83  ioe.What() ) );
84  return;
85  }
86 
87  if( m_footprint )
88  {
89  wxString name = m_lib_id.GetLibItemName();
90  wxString desc = m_footprint->GetDescription();
91  wxString keywords = m_footprint->GetKeywords();
92  wxString doc;
93 
94  // It is currently common practice to store a documentation link in the description.
95  int idx = desc.find( wxT( "http:" ) );
96 
97  if( idx >= 0 )
98  {
99  doc = desc.substr( (unsigned) idx );
100 
101  desc = desc.substr( 0, (unsigned) idx );
102  desc = desc.Trim( true );
103 
104  if( !desc.IsEmpty() && desc.Last() == ',' )
105  desc.RemoveLast( 1 );
106  }
107 
108  m_html.Replace( "__NAME__", EscapeHTML( name ) );
109  m_html.Replace( "__DESC__", EscapeHTML( desc ) );
110 
111  wxString keywordsHtml = KeywordsFormat;
112  keywordsHtml.Replace( "__KEYWORDS__", EscapeHTML( keywords ) );
113 
114  wxString docHtml = DocFormat;
115  docHtml.Replace( "__HREF__", EscapeHTML( doc ) );
116 
117  if( doc.Length() > 75 )
118  doc = doc.Left( 72 ) + wxT( "..." );
119 
120  docHtml.Replace( "__TEXT__", EscapeHTML( doc ) );
121 
122  m_html.Replace( "__FIELDS__", keywordsHtml + docHtml );
123  }
124  }
125 
129  wxString GetHtml()
130  {
131  return m_html;
132  }
133 
134 };
135 
136 
137 wxString GenerateFootprintInfo( FP_LIB_TABLE* aFpLibTable, LIB_ID const& aLibId )
138 {
139  FOOTPRINT_INFO_GENERATOR gen( aFpLibTable, aLibId );
140  gen.GenerateHtml();
141  return gen.GetHtml();
142 }
const wxString & GetDescription() const
Definition: footprint.h:193
const UTF8 & GetLibItemName() const
Definition: lib_id.h:106
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:168
#define gen(mvar, chg_bit, get, set, add, sub, changed)
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:29
FOOTPRINT_INFO_GENERATOR(FP_LIB_TABLE *aFpLibTable, LIB_ID const &aLibId)
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:92
const wxString & GetKeywords() const
Definition: footprint.h:196
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.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
const char * name
Definition: DXF_plotter.cpp:59
#define _(s)
Definition: 3d_actions.cpp:33
wxString wx_str() const
Definition: utf8.cpp:51
static const wxString DescriptionFormat
wxString EscapeHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
Definition: string.cpp:346
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75