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.

179 lines
4.6 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_alias_info.h>
  21. #include <kicad_string.h>
  22. #include <template_fieldnames.h>
  23. static const wxString DescriptionFormat =
  24. "<b>__NAME__</b>"
  25. "__ALIASOF__"
  26. "__DESC__"
  27. "__KEY__"
  28. "<hr><table border=0>"
  29. "__FIELDS__"
  30. "</table>";
  31. static const wxString AliasOfFormat = "<br><i>" + _( "Alias of " ) + "%s (%s)</i>";
  32. static const wxString DescFormat = "<br>%s";
  33. static const wxString KeywordsFormat = "<br>" + _( "Keywords:" ) + " %s";
  34. static const wxString FieldFormat =
  35. "<tr>"
  36. " <td><b>__NAME__</b></td>"
  37. " <td>__VALUE__</td>"
  38. "</tr>";
  39. static const wxString DatasheetLinkFormat = "<a href=\"__VALUE__\">__VALUE__</a>";
  40. class ALIAS_INFO_GENERATOR
  41. {
  42. wxString m_html;
  43. LIB_ALIAS const * m_part;
  44. int m_unit;
  45. public:
  46. ALIAS_INFO_GENERATOR( LIB_ALIAS const * aAlias, int aUnit )
  47. : m_html( DescriptionFormat ),
  48. m_part( aAlias ),
  49. m_unit( aUnit )
  50. { }
  51. /**
  52. * Generate the HTML internally.
  53. */
  54. void GenerateHtml()
  55. {
  56. SetHtmlName();
  57. SetHtmlAliasOf();
  58. SetHtmlDesc();
  59. SetHtmlKeywords();
  60. SetHtmlFieldTable();
  61. }
  62. /**
  63. * Return the generated HTML.
  64. */
  65. wxString GetHtml()
  66. {
  67. return m_html;
  68. }
  69. protected:
  70. void SetHtmlName()
  71. {
  72. m_html.Replace( "__NAME__", EscapedHTML( m_part->GetName() ) );
  73. }
  74. void SetHtmlAliasOf()
  75. {
  76. if( m_part->IsRoot() )
  77. {
  78. m_html.Replace( "__ALIASOF__", wxEmptyString );
  79. }
  80. else
  81. {
  82. wxString root_name = _( "Unknown" );
  83. wxString root_desc = "";
  84. LIB_PART* root = m_part->GetPart();
  85. LIB_ALIAS* root_alias = root ? root->GetAlias( 0 ) : nullptr;
  86. if( root )
  87. root_name = root->GetName();
  88. if( root_alias )
  89. root_desc = root_alias->GetDescription();
  90. m_html.Replace(
  91. "__ALIASOF__", wxString::Format(
  92. AliasOfFormat, EscapedHTML( root_name ), EscapedHTML( root_desc ) ) );
  93. }
  94. }
  95. void SetHtmlDesc()
  96. {
  97. wxString raw_desc = m_part->GetDescription();
  98. m_html.Replace( "__DESC__", wxString::Format( DescFormat, EscapedHTML( raw_desc ) ) );
  99. }
  100. void SetHtmlKeywords()
  101. {
  102. wxString keywords = m_part->GetKeyWords();
  103. if( keywords.empty() )
  104. m_html.Replace( "__KEY__", wxEmptyString );
  105. else
  106. m_html.Replace( "__KEY__",
  107. wxString::Format( KeywordsFormat, EscapedHTML( keywords ) ) );
  108. }
  109. wxString GetHtmlFieldRow( LIB_FIELD const & aField )
  110. {
  111. wxString name = aField.GetName();
  112. wxString text = aField.GetFullText( m_unit > 0 ? m_unit : 1 );
  113. wxString fieldhtml = FieldFormat;
  114. fieldhtml.Replace( "__NAME__", EscapedHTML( name ) );
  115. switch( aField.GetId() )
  116. {
  117. case DATASHEET:
  118. {
  119. wxString datasheetlink = DatasheetLinkFormat;
  120. datasheetlink.Replace( "__VALUE__", EscapedHTML( text ) );
  121. fieldhtml.Replace( "__VALUE__", datasheetlink );
  122. }
  123. break;
  124. default:
  125. fieldhtml.Replace( "__VALUE__", EscapedHTML( text ) );
  126. }
  127. return fieldhtml;
  128. }
  129. void SetHtmlFieldTable()
  130. {
  131. wxString fieldtable;
  132. LIB_FIELDS fields;
  133. m_part->GetPart()->GetFields( fields );
  134. for( auto const & field: fields )
  135. {
  136. fieldtable += GetHtmlFieldRow( field );
  137. }
  138. m_html.Replace( "__FIELDS__", fieldtable );
  139. }
  140. };
  141. wxString GenerateAliasInfo( LIB_ALIAS const * aAlias, int aUnit )
  142. {
  143. ALIAS_INFO_GENERATOR gen( aAlias, aUnit );
  144. gen.GenerateHtml();
  145. return gen.GetHtml();
  146. }