You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
4.7 KiB

  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. #include <generate_footprint_info.h>
  21. #include <string_utils.h>
  22. #include <footprint.h>
  23. #include <fp_lib_table.h>
  24. #include <wx/log.h>
  25. static const wxString DescriptionFormat = wxT(
  26. "<b>__NAME__</b>"
  27. "<br>__DESC__"
  28. "<hr><table border=0>"
  29. "__FIELDS__"
  30. "</table>" );
  31. static const wxString KeywordsFormat = wxT(
  32. "<tr>"
  33. " <td><b>" + _( "Keywords" ) + "</b></td>"
  34. " <td>__KEYWORDS__</td>"
  35. "</tr>" );
  36. static const wxString DocFormat = wxT(
  37. "<tr>"
  38. " <td><b>" + _( "Documentation" ) + "</b></td>"
  39. " <td><a href=\"__HREF__\">__TEXT__</a></td>"
  40. "</tr>" );
  41. class FOOTPRINT_INFO_GENERATOR
  42. {
  43. wxString m_html;
  44. FP_LIB_TABLE* m_fp_lib_table;
  45. LIB_ID const m_lib_id;
  46. const FOOTPRINT* m_footprint;
  47. public:
  48. FOOTPRINT_INFO_GENERATOR( FP_LIB_TABLE* aFpLibTable, LIB_ID const& aLibId )
  49. : m_html( DescriptionFormat ),
  50. m_fp_lib_table( aFpLibTable ),
  51. m_lib_id( aLibId ),
  52. m_footprint( nullptr )
  53. { }
  54. /**
  55. * Generate the HTML internally.
  56. */
  57. void GenerateHtml()
  58. {
  59. wxCHECK_RET( m_fp_lib_table, wxT( "Footprint library table pointer is not valid" ) );
  60. if( !m_lib_id.IsValid() )
  61. return;
  62. try
  63. {
  64. m_footprint = m_fp_lib_table->GetEnumeratedFootprint( m_lib_id.GetLibNickname(),
  65. m_lib_id.GetLibItemName() );
  66. }
  67. catch( const IO_ERROR& ioe )
  68. {
  69. wxLogError( _( "Error loading footprint %s from library '%s'." ) + wxS( "\n%s" ),
  70. m_lib_id.GetLibItemName().wx_str(),
  71. m_lib_id.GetLibNickname().wx_str(),
  72. ioe.What() );
  73. return;
  74. }
  75. if( m_footprint )
  76. {
  77. wxString name = m_lib_id.GetLibItemName();
  78. wxString desc = m_footprint->GetLibDescription();
  79. wxString keywords = m_footprint->GetKeywords();
  80. wxString doc;
  81. // It is currently common practice to store a documentation link in the description.
  82. int idx = desc.find( wxT( "http:" ) );
  83. if( idx < 0 )
  84. idx = desc.find( wxT( "https:" ) );
  85. if( idx >= 0 )
  86. {
  87. int nesting = 0;
  88. for( auto chit = desc.begin() + idx; chit != desc.end(); ++chit )
  89. {
  90. int ch = *chit;
  91. // Break on invalid URI characters
  92. if( ch <= 0x20 || ch >= 0x7F || ch == '"' )
  93. break;
  94. // Check for nesting parentheses, e.g. (Body style from: https://this.url/part.pdf)
  95. if( ch == '(' )
  96. ++nesting;
  97. else if( ch == ')' && --nesting < 0 )
  98. break;
  99. doc += ch;
  100. }
  101. desc.Replace( doc, _( "doc url" ) );
  102. }
  103. m_html.Replace( "__NAME__", EscapeHTML( name ) );
  104. m_html.Replace( "__DESC__", EscapeHTML( desc ) );
  105. wxString keywordsHtml = KeywordsFormat;
  106. keywordsHtml.Replace( "__KEYWORDS__", EscapeHTML( keywords ) );
  107. wxString docHtml = DocFormat;
  108. docHtml.Replace( "__HREF__", EscapeHTML( doc ) );
  109. if( doc.Length() > 75 )
  110. doc = doc.Left( 72 ) + wxT( "..." );
  111. docHtml.Replace( "__TEXT__", EscapeHTML( doc ) );
  112. m_html.Replace( "__FIELDS__", keywordsHtml + docHtml );
  113. }
  114. }
  115. /**
  116. * Return the generated HTML.
  117. */
  118. wxString GetHtml()
  119. {
  120. return m_html;
  121. }
  122. };
  123. wxString GenerateFootprintInfo( FP_LIB_TABLE* aFpLibTable, LIB_ID const& aLibId )
  124. {
  125. FOOTPRINT_INFO_GENERATOR gen( aFpLibTable, aLibId );
  126. gen.GenerateHtml();
  127. return gen.GetHtml();
  128. }