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.

141 lines
4.2 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 <kicad_string.h>
  22. #include <class_module.h>
  23. #include <fp_lib_table.h>
  24. static const wxString DescriptionFormat =
  25. "<b>__NAME__</b>"
  26. "<br>__DESC__"
  27. "<hr><table border=0>"
  28. "__FIELDS__"
  29. "</table>";
  30. static const wxString KeywordsFormat =
  31. "<tr>"
  32. " <td><b>" + _( "Keywords" ) + "</b></td>"
  33. " <td>__KEYWORDS__</td>"
  34. "</tr>";
  35. static const wxString DocFormat =
  36. "<tr>"
  37. " <td><b>" + _( "Documentation" ) + "</b></td>"
  38. " <td><a href=\"__HREF__\">__TEXT__</a></td>"
  39. "</tr>";
  40. class FOOTPRINT_INFO_GENERATOR
  41. {
  42. wxString m_html;
  43. FP_LIB_TABLE* m_fp_lib_table;
  44. LIB_ID const m_lib_id;
  45. MODULE* m_module;
  46. public:
  47. FOOTPRINT_INFO_GENERATOR( FP_LIB_TABLE* aFpLibTable, LIB_ID const& aLibId )
  48. : m_html( DescriptionFormat ),
  49. m_fp_lib_table( aFpLibTable ),
  50. m_lib_id( aLibId ),
  51. m_module( nullptr )
  52. { }
  53. /**
  54. * Generate the HTML internally.
  55. */
  56. void GenerateHtml()
  57. {
  58. wxCHECK_RET( m_fp_lib_table, "Footprint library table pointer is not valid" );
  59. if( !m_lib_id.IsValid() )
  60. return;
  61. try
  62. {
  63. m_module = m_fp_lib_table->FootprintLoad( m_lib_id.GetLibNickname(),
  64. m_lib_id.GetLibItemName() );
  65. }
  66. catch( const IO_ERROR& ioe )
  67. {
  68. wxLogError( wxString::Format( _( "Error loading footprint %s from library %s.\n\n%s" ),
  69. m_lib_id.GetLibItemName().wx_str(),
  70. m_lib_id.GetLibNickname().wx_str(),
  71. ioe.What() ) );
  72. return;
  73. }
  74. if( m_module )
  75. {
  76. wxString name = m_lib_id.GetLibItemName();
  77. wxString desc = m_module->GetDescription();
  78. wxString keywords = m_module->GetKeywords();
  79. wxString doc;
  80. // It is currently common practice to store a documentation link in the description.
  81. int idx = desc.find( wxT( "http:" ) );
  82. if( idx >= 0 )
  83. {
  84. doc = desc.substr( (unsigned) idx );
  85. desc = desc.substr( 0, (unsigned) idx );
  86. desc = desc.Trim( true );
  87. if( !desc.IsEmpty() && desc.Last() == ',' )
  88. desc.RemoveLast( 1 );
  89. }
  90. m_html.Replace( "__NAME__", EscapedHTML( name ) );
  91. m_html.Replace( "__DESC__", EscapedHTML( desc ) );
  92. wxString keywordsHtml = KeywordsFormat;
  93. keywordsHtml.Replace( "__KEYWORDS__", EscapedHTML( keywords ) );
  94. wxString docHtml = DocFormat;
  95. docHtml.Replace( "__HREF__", EscapedHTML( doc ) );
  96. if( doc.Length() > 75 )
  97. doc = doc.Left( 72 ) + wxT( "..." );
  98. docHtml.Replace( "__TEXT__", EscapedHTML( doc ) );
  99. m_html.Replace( "__FIELDS__", keywordsHtml + docHtml );
  100. }
  101. }
  102. /**
  103. * Return the generated HTML.
  104. */
  105. wxString GetHtml()
  106. {
  107. return m_html;
  108. }
  109. };
  110. wxString GenerateFootprintInfo( FP_LIB_TABLE* aFpLibTable, LIB_ID const& aLibId )
  111. {
  112. FOOTPRINT_INFO_GENERATOR gen( aFpLibTable, aLibId );
  113. gen.GenerateHtml();
  114. return gen.GetHtml();
  115. }