2018-07-29 01:11:39 +01:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017 Chris Pavlina <pavlina.chris@gmail.com>
|
2025-01-01 13:30:11 -08:00
|
|
|
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
|
2018-07-29 01:11:39 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <generate_footprint_info.h>
|
2021-07-29 10:56:22 +01:00
|
|
|
#include <string_utils.h>
|
2020-11-12 20:19:22 +00:00
|
|
|
#include <footprint.h>
|
2018-07-29 01:11:39 +01:00
|
|
|
#include <fp_lib_table.h>
|
2021-06-01 01:17:57 -04:00
|
|
|
#include <wx/log.h>
|
2018-07-29 01:11:39 +01:00
|
|
|
|
|
|
|
|
2022-02-04 22:44:59 +00:00
|
|
|
static const wxString DescriptionFormat = wxT(
|
2018-07-29 01:11:39 +01:00
|
|
|
"<b>__NAME__</b>"
|
|
|
|
"<br>__DESC__"
|
|
|
|
"<hr><table border=0>"
|
|
|
|
"__FIELDS__"
|
2022-02-04 22:44:59 +00:00
|
|
|
"</table>" );
|
2018-07-29 01:11:39 +01:00
|
|
|
|
2022-02-04 22:44:59 +00:00
|
|
|
static const wxString KeywordsFormat = wxT(
|
2018-07-29 01:11:39 +01:00
|
|
|
"<tr>"
|
|
|
|
" <td><b>" + _( "Keywords" ) + "</b></td>"
|
|
|
|
" <td>__KEYWORDS__</td>"
|
2022-02-04 22:44:59 +00:00
|
|
|
"</tr>" );
|
2018-07-29 01:11:39 +01:00
|
|
|
|
2022-02-04 22:44:59 +00:00
|
|
|
static const wxString DocFormat = wxT(
|
2018-07-29 01:11:39 +01:00
|
|
|
"<tr>"
|
|
|
|
" <td><b>" + _( "Documentation" ) + "</b></td>"
|
|
|
|
" <td><a href=\"__HREF__\">__TEXT__</a></td>"
|
2022-02-04 22:44:59 +00:00
|
|
|
"</tr>" );
|
2018-07-29 01:11:39 +01:00
|
|
|
|
|
|
|
|
2024-10-14 23:13:44 +08:00
|
|
|
std::optional<wxString> GetFootprintDocumentationURL( const FOOTPRINT& aFootprint )
|
|
|
|
{
|
2025-06-17 18:02:40 +02:00
|
|
|
// Footprints have now a field (FIELD_T::DATASHEET) containing the url datasheet
|
|
|
|
// But old footprints did not have this field, so this fiels can be empty.
|
|
|
|
// So we use this field is not empty, and if empty see if the documentation has an URL
|
|
|
|
const PCB_FIELD* data_field = aFootprint.GetField( FIELD_T::DATASHEET );
|
|
|
|
wxString url = data_field->GetText();
|
|
|
|
|
|
|
|
if( !url.IsEmpty() )
|
|
|
|
return url;
|
2024-10-14 23:13:44 +08:00
|
|
|
|
2025-06-17 18:02:40 +02:00
|
|
|
wxString desc = aFootprint.GetLibDescription();
|
2024-10-14 23:13:44 +08:00
|
|
|
|
2025-06-17 18:02:40 +02:00
|
|
|
// It is (or was) currently common practice to store a documentation link in the description.
|
2024-10-14 23:13:44 +08:00
|
|
|
size_t idx = desc.find( wxT( "http:" ) );
|
|
|
|
|
|
|
|
if( idx == wxString::npos )
|
|
|
|
idx = desc.find( wxT( "https:" ) );
|
|
|
|
|
|
|
|
if( idx == wxString::npos )
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
int nesting = 0;
|
|
|
|
|
|
|
|
for( auto chit = desc.begin() + idx; chit != desc.end(); ++chit )
|
|
|
|
{
|
|
|
|
int ch = *chit;
|
|
|
|
|
|
|
|
// Break on invalid URI characters
|
|
|
|
if( ch <= 0x20 || ch >= 0x7F || ch == '"' )
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Check for nesting parentheses, e.g. (Body style from: https://this.url/part.pdf)
|
|
|
|
if( ch == '(' )
|
|
|
|
++nesting;
|
|
|
|
else if( ch == ')' && --nesting < 0 )
|
|
|
|
break;
|
|
|
|
|
|
|
|
url += ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trim trailing punctuation
|
|
|
|
static wxString punct = wxS( ".,:;" );
|
|
|
|
|
|
|
|
if( punct.find( url.Last() ) != wxString::npos )
|
|
|
|
url = url.Left( url.Length() - 1 );
|
|
|
|
|
|
|
|
if( url.IsEmpty() )
|
|
|
|
return std::nullopt;
|
2025-06-17 18:02:40 +02:00
|
|
|
|
2024-10-14 23:13:44 +08:00
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-29 01:11:39 +01:00
|
|
|
class FOOTPRINT_INFO_GENERATOR
|
|
|
|
{
|
|
|
|
public:
|
2025-07-13 14:56:22 +01:00
|
|
|
FOOTPRINT_INFO_GENERATOR( FP_LIB_TABLE* aFpLibTable, LIB_ID const& aLibId ) :
|
|
|
|
m_html( DescriptionFormat ),
|
|
|
|
m_fp_lib_table( aFpLibTable ),
|
|
|
|
m_lib_id( aLibId ),
|
|
|
|
m_footprint( nullptr )
|
2018-07-29 01:11:39 +01:00
|
|
|
{ }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the HTML internally.
|
|
|
|
*/
|
|
|
|
void GenerateHtml()
|
|
|
|
{
|
2022-02-04 22:44:59 +00:00
|
|
|
wxCHECK_RET( m_fp_lib_table, wxT( "Footprint library table pointer is not valid" ) );
|
2018-07-29 01:11:39 +01:00
|
|
|
|
|
|
|
if( !m_lib_id.IsValid() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2021-02-25 15:42:12 -08:00
|
|
|
m_footprint = m_fp_lib_table->GetEnumeratedFootprint( m_lib_id.GetLibNickname(),
|
2025-07-13 14:56:22 +01:00
|
|
|
m_lib_id.GetLibItemName(),
|
|
|
|
false );
|
2018-07-29 01:11:39 +01:00
|
|
|
}
|
|
|
|
catch( const IO_ERROR& ioe )
|
|
|
|
{
|
2021-06-26 20:21:30 +01:00
|
|
|
wxLogError( _( "Error loading footprint %s from library '%s'." ) + wxS( "\n%s" ),
|
|
|
|
m_lib_id.GetLibItemName().wx_str(),
|
|
|
|
m_lib_id.GetLibNickname().wx_str(),
|
|
|
|
ioe.What() );
|
2018-07-29 01:11:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-10 21:20:03 +00:00
|
|
|
if( m_footprint )
|
2018-07-29 01:11:39 +01:00
|
|
|
{
|
|
|
|
wxString name = m_lib_id.GetLibItemName();
|
2023-06-19 13:08:18 -04:00
|
|
|
wxString desc = m_footprint->GetLibDescription();
|
2020-11-10 21:20:03 +00:00
|
|
|
wxString keywords = m_footprint->GetKeywords();
|
2018-07-29 01:11:39 +01:00
|
|
|
wxString doc;
|
|
|
|
|
2024-10-14 23:13:44 +08:00
|
|
|
if( std::optional<wxString> url = GetFootprintDocumentationURL( *m_footprint ) )
|
|
|
|
doc = *url;
|
2018-07-29 01:11:39 +01:00
|
|
|
|
2023-10-15 01:00:02 +03:00
|
|
|
wxString esc_desc = EscapeHTML( UnescapeString( desc ) );
|
|
|
|
|
|
|
|
// Add line breaks
|
|
|
|
esc_desc.Replace( wxS( "\n" ), wxS( "<br>" ) );
|
|
|
|
|
|
|
|
// Add links
|
|
|
|
esc_desc = LinkifyHTML( esc_desc );
|
|
|
|
|
|
|
|
m_html.Replace( "__DESC__", esc_desc );
|
2020-11-30 12:14:22 +00:00
|
|
|
m_html.Replace( "__NAME__", EscapeHTML( name ) );
|
2018-07-29 01:11:39 +01:00
|
|
|
|
|
|
|
wxString keywordsHtml = KeywordsFormat;
|
2020-11-30 12:14:22 +00:00
|
|
|
keywordsHtml.Replace( "__KEYWORDS__", EscapeHTML( keywords ) );
|
2018-07-29 01:11:39 +01:00
|
|
|
|
|
|
|
wxString docHtml = DocFormat;
|
2020-11-30 12:14:22 +00:00
|
|
|
docHtml.Replace( "__HREF__", EscapeHTML( doc ) );
|
2018-07-29 01:11:39 +01:00
|
|
|
|
|
|
|
if( doc.Length() > 75 )
|
|
|
|
doc = doc.Left( 72 ) + wxT( "..." );
|
|
|
|
|
2020-11-30 12:14:22 +00:00
|
|
|
docHtml.Replace( "__TEXT__", EscapeHTML( doc ) );
|
2018-07-29 01:11:39 +01:00
|
|
|
|
|
|
|
m_html.Replace( "__FIELDS__", keywordsHtml + docHtml );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the generated HTML.
|
|
|
|
*/
|
|
|
|
wxString GetHtml()
|
|
|
|
{
|
|
|
|
return m_html;
|
|
|
|
}
|
|
|
|
|
2025-07-13 14:56:22 +01:00
|
|
|
private:
|
|
|
|
wxString m_html;
|
|
|
|
FP_LIB_TABLE* m_fp_lib_table;
|
|
|
|
LIB_ID const m_lib_id;
|
|
|
|
|
|
|
|
const FOOTPRINT* m_footprint;
|
2018-07-29 01:11:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-08-05 12:56:02 +01:00
|
|
|
wxString GenerateFootprintInfo( FP_LIB_TABLE* aFpLibTable, LIB_ID const& aLibId )
|
2018-07-29 01:11:39 +01:00
|
|
|
{
|
2018-08-05 12:56:02 +01:00
|
|
|
FOOTPRINT_INFO_GENERATOR gen( aFpLibTable, aLibId );
|
2018-07-29 01:11:39 +01:00
|
|
|
gen.GenerateHtml();
|
|
|
|
return gen.GetHtml();
|
|
|
|
}
|