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 <class_module.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;
53 
54 public:
55  FOOTPRINT_INFO_GENERATOR( FP_LIB_TABLE* aFpLibTable, LIB_ID const& aLibId )
56  : m_html( DescriptionFormat ),
57  m_fp_lib_table( aFpLibTable ),
58  m_lib_id( aLibId ),
59  m_module( nullptr )
60  { }
61 
65  void GenerateHtml()
66  {
67  wxCHECK_RET( m_fp_lib_table, "Footprint library table pointer is not valid" );
68 
69  if( !m_lib_id.IsValid() )
70  return;
71 
72  try
73  {
74  m_module = m_fp_lib_table->FootprintLoad( m_lib_id.GetLibNickname(),
75  m_lib_id.GetLibItemName() );
76  }
77  catch( const IO_ERROR& ioe )
78  {
79  wxLogError( wxString::Format( _( "Error loading footprint %s from library %s.\n\n%s" ),
80  m_lib_id.GetLibItemName().wx_str(),
81  m_lib_id.GetLibNickname().wx_str(),
82  ioe.What() ) );
83  return;
84  }
85 
86  if( m_module )
87  {
88  wxString name = m_lib_id.GetLibItemName();
89  wxString desc = m_module->GetDescription();
90  wxString keywords = m_module->GetKeywords();
91  wxString doc;
92 
93  // It is currently common practice to store a documentation link in the description.
94  int idx = desc.find( wxT( "http:" ) );
95 
96  if( idx >= 0 )
97  {
98  doc = desc.substr( (unsigned) idx );
99 
100  desc = desc.substr( 0, (unsigned) idx );
101  desc = desc.Trim( true );
102 
103  if( !desc.IsEmpty() && desc.Last() == ',' )
104  desc.RemoveLast( 1 );
105  }
106 
107  m_html.Replace( "__NAME__", EscapedHTML( name ) );
108  m_html.Replace( "__DESC__", EscapedHTML( desc ) );
109 
110  wxString keywordsHtml = KeywordsFormat;
111  keywordsHtml.Replace( "__KEYWORDS__", EscapedHTML( keywords ) );
112 
113  wxString docHtml = DocFormat;
114  docHtml.Replace( "__HREF__", EscapedHTML( doc ) );
115 
116  if( doc.Length() > 75 )
117  doc = doc.Left( 72 ) + wxT( "..." );
118 
119  docHtml.Replace( "__TEXT__", EscapedHTML( doc ) );
120 
121  m_html.Replace( "__FIELDS__", keywordsHtml + docHtml );
122  }
123  }
124 
128  wxString GetHtml()
129  {
130  return m_html;
131  }
132 
133 };
134 
135 
136 wxString GenerateFootprintInfo( FP_LIB_TABLE* aFpLibTable, LIB_ID const& aLibId )
137 {
138  FOOTPRINT_INFO_GENERATOR gen( aFpLibTable, aLibId );
139  gen.GenerateHtml();
140  return gen.GetHtml();
141 }
bool IsValid() const
Definition: lib_id.h:171
void GenerateHtml()
Generate the HTML internally.
static const wxString KeywordsFormat
static const wxString DocFormat
wxString EscapedHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
Definition: string.cpp:247
MODULE * FootprintLoad(const wxString &aNickname, const wxString &aFootprintName)
Function FootprintLoad.
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
wxString wx_str() const
Definition: utf8.cpp:51
FOOTPRINT_INFO_GENERATOR(FP_LIB_TABLE *aFpLibTable, LIB_ID const &aLibId)
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
const wxString & GetKeywords() const
Definition: class_module.h:199
wxString GetHtml()
Return the generated HTML.
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
const char * name
Definition: DXF_plotter.cpp:61
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
const wxString & GetDescription() const
Definition: class_module.h:196
Module description (excepted pads)
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:97
static const wxString DescriptionFormat
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76