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.

169 lines
5.0 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->GetDescription();
  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. // And, sadly, it appears to have also become customary to bury the url inside
  88. // parentheses.
  89. if( idx >= 1 && desc.at( idx - 1 ) == '(' )
  90. {
  91. int nesting = 0;
  92. while( idx < (int) desc.size() )
  93. {
  94. char c = desc.at( idx++ );
  95. if( c == '(' )
  96. nesting++;
  97. else if( c == ')' && --nesting < 0 )
  98. break;
  99. doc += c;
  100. }
  101. desc.Replace( doc, _( "doc url" ) );
  102. }
  103. else
  104. {
  105. doc = desc.substr( (unsigned) idx );
  106. desc = desc.substr( 0, (unsigned) idx );
  107. desc = desc.Trim( true );
  108. if( !desc.IsEmpty() && desc.Last() == ',' )
  109. desc.RemoveLast( 1 );
  110. }
  111. }
  112. m_html.Replace( "__NAME__", EscapeHTML( name ) );
  113. m_html.Replace( "__DESC__", EscapeHTML( desc ) );
  114. wxString keywordsHtml = KeywordsFormat;
  115. keywordsHtml.Replace( "__KEYWORDS__", EscapeHTML( keywords ) );
  116. wxString docHtml = DocFormat;
  117. docHtml.Replace( "__HREF__", EscapeHTML( doc ) );
  118. if( doc.Length() > 75 )
  119. doc = doc.Left( 72 ) + wxT( "..." );
  120. docHtml.Replace( "__TEXT__", EscapeHTML( doc ) );
  121. m_html.Replace( "__FIELDS__", keywordsHtml + docHtml );
  122. }
  123. }
  124. /**
  125. * Return the generated HTML.
  126. */
  127. wxString GetHtml()
  128. {
  129. return m_html;
  130. }
  131. };
  132. wxString GenerateFootprintInfo( FP_LIB_TABLE* aFpLibTable, LIB_ID const& aLibId )
  133. {
  134. FOOTPRINT_INFO_GENERATOR gen( aFpLibTable, aLibId );
  135. gen.GenerateHtml();
  136. return gen.GetHtml();
  137. }